SDL_hidapi_ps3.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_JOYSTICK_HIDAPI
  20. #include "../../SDL_hints_c.h"
  21. #include "../SDL_sysjoystick.h"
  22. #include "SDL_hidapijoystick_c.h"
  23. #include "SDL_hidapi_rumble.h"
  24. #ifdef SDL_JOYSTICK_HIDAPI_PS3
  25. // Define this if you want to log all packets from the controller
  26. // #define DEBUG_PS3_PROTOCOL
  27. #define LOAD16(A, B) (Sint16)((Uint16)(A) | (((Uint16)(B)) << 8))
  28. typedef enum
  29. {
  30. k_EPS3ReportIdState = 1,
  31. k_EPS3ReportIdEffects = 1,
  32. } EPS3ReportId;
  33. typedef enum
  34. {
  35. k_EPS3SonySixaxisReportIdState = 0,
  36. k_EPS3SonySixaxisReportIdEffects = 0,
  37. } EPS3SonySixaxisReportId;
  38. // Commands for Sony's sixaxis.sys Windows driver
  39. // All commands must be sent using 49-byte buffer containing output report
  40. // Byte 0 indicates reportId and must always be 0
  41. // Byte 1 indicates a command, supported values are specified below:
  42. typedef enum
  43. {
  44. // This command allows to set user LEDs.
  45. // Bytes 5,6.7.8 contain mode for corresponding LED: 0 - LED is off, 1 - LED in on, 2 - LED is flashing.
  46. // Bytes 9-16 specify 64-bit LED flash period in 100 ns units if some LED is flashing, otherwise not used.
  47. k_EPS3SixaxisCommandSetLEDs = 1,
  48. // This command allows to set left and right motors.
  49. // Byte 5 is right motor duration (0-255) and byte 6, if not zero, activates right motor. Zero value disables right motor.
  50. // Byte 7 is left motor duration (0-255) and byte 8 is left motor amplitude (0-255).
  51. k_EPS3SixaxisCommandSetMotors = 2,
  52. // This command allows to block/unblock setting device LEDs by applications.
  53. // Byte 5 is used as parameter - any non-zero value blocks LEDs, zero value will unblock LEDs.
  54. k_EPS3SixaxisCommandBlockLEDs = 3,
  55. // This command refreshes driver settings. No parameters used.
  56. // When sixaxis driver loads it reads 'CurrentDriverSetting' binary value from 'HKLM\System\CurrentControlSet\Services\sixaxis\Parameters' registry key.
  57. // If the key is not present then default values are used. Sending this command forces sixaxis driver to re-read the registry and update driver settings.
  58. k_EPS3SixaxisCommandRefreshDriverSetting = 9,
  59. // This command clears current bluetooth pairing. No parameters used.
  60. k_EPS3SixaxisCommandClearPairing = 10
  61. } EPS3SixaxisDriverCommands;
  62. typedef struct
  63. {
  64. SDL_HIDAPI_Device *device;
  65. SDL_Joystick *joystick;
  66. bool is_shanwan;
  67. bool has_analog_buttons;
  68. bool report_sensors;
  69. bool effects_updated;
  70. int player_index;
  71. Uint8 rumble_left;
  72. Uint8 rumble_right;
  73. Uint8 last_state[USB_PACKET_LENGTH];
  74. } SDL_DriverPS3_Context;
  75. static bool HIDAPI_DriverPS3_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size);
  76. static void HIDAPI_DriverPS3_RegisterHints(SDL_HintCallback callback, void *userdata)
  77. {
  78. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS3, callback, userdata);
  79. }
  80. static void HIDAPI_DriverPS3_UnregisterHints(SDL_HintCallback callback, void *userdata)
  81. {
  82. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS3, callback, userdata);
  83. }
  84. static bool HIDAPI_DriverPS3_IsEnabled(void)
  85. {
  86. bool default_value;
  87. #ifdef SDL_PLATFORM_MACOS
  88. // This works well on macOS
  89. default_value = true;
  90. #elif defined(SDL_PLATFORM_WIN32)
  91. /* For official Sony driver (sixaxis.sys) use SDL_HINT_JOYSTICK_HIDAPI_PS3_SIXAXIS_DRIVER.
  92. *
  93. * See https://github.com/ViGEm/DsHidMini as an alternative driver
  94. */
  95. default_value = false;
  96. #elif defined(SDL_PLATFORM_LINUX)
  97. /* Linux drivers do a better job of managing the transition between
  98. * USB and Bluetooth. There are also some quirks in communicating
  99. * with PS3 controllers that have been implemented in SDL's hidapi
  100. * for libusb, but are not possible to support using hidraw if the
  101. * kernel doesn't already know about them.
  102. */
  103. default_value = false;
  104. #else
  105. // Untested, default off
  106. default_value = false;
  107. #endif
  108. if (default_value) {
  109. default_value = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT);
  110. }
  111. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS3, default_value);
  112. }
  113. static bool HIDAPI_DriverPS3_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)
  114. {
  115. if (vendor_id == USB_VENDOR_SONY && product_id == USB_PRODUCT_SONY_DS3) {
  116. return true;
  117. }
  118. if (vendor_id == USB_VENDOR_SHANWAN && product_id == USB_PRODUCT_SHANWAN_DS3) {
  119. return true;
  120. }
  121. return false;
  122. }
  123. static int ReadFeatureReport(SDL_hid_device *dev, Uint8 report_id, Uint8 *report, size_t length)
  124. {
  125. SDL_memset(report, 0, length);
  126. report[0] = report_id;
  127. return SDL_hid_get_feature_report(dev, report, length);
  128. }
  129. static int SendFeatureReport(SDL_hid_device *dev, Uint8 *report, size_t length)
  130. {
  131. return SDL_hid_send_feature_report(dev, report, length);
  132. }
  133. static bool HIDAPI_DriverPS3_InitDevice(SDL_HIDAPI_Device *device)
  134. {
  135. SDL_DriverPS3_Context *ctx;
  136. bool is_shanwan = false;
  137. if (device->vendor_id == USB_VENDOR_SONY &&
  138. SDL_strncasecmp(device->name, "ShanWan", 7) == 0) {
  139. is_shanwan = true;
  140. }
  141. if (device->vendor_id == USB_VENDOR_SHANWAN ||
  142. device->vendor_id == USB_VENDOR_SHANWAN_ALT) {
  143. is_shanwan = true;
  144. }
  145. ctx = (SDL_DriverPS3_Context *)SDL_calloc(1, sizeof(*ctx));
  146. if (!ctx) {
  147. return false;
  148. }
  149. ctx->device = device;
  150. ctx->is_shanwan = is_shanwan;
  151. ctx->has_analog_buttons = true;
  152. device->context = ctx;
  153. // Set the controller into report mode over Bluetooth
  154. if (device->is_bluetooth) {
  155. Uint8 data[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
  156. SendFeatureReport(device->dev, data, sizeof(data));
  157. }
  158. // Set the controller into report mode over USB
  159. if (!device->is_bluetooth) {
  160. Uint8 data[USB_PACKET_LENGTH];
  161. int size = ReadFeatureReport(device->dev, 0xf2, data, 17);
  162. if (size < 0) {
  163. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  164. "HIDAPI_DriverPS3_InitDevice(): Couldn't read feature report 0xf2");
  165. return false;
  166. }
  167. #ifdef DEBUG_PS3_PROTOCOL
  168. HIDAPI_DumpPacket("PS3 0xF2 packet: size = %d", data, size);
  169. #endif
  170. size = ReadFeatureReport(device->dev, 0xf5, data, 8);
  171. if (size < 0) {
  172. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  173. "HIDAPI_DriverPS3_InitDevice(): Couldn't read feature report 0xf5");
  174. return false;
  175. }
  176. #ifdef DEBUG_PS3_PROTOCOL
  177. HIDAPI_DumpPacket("PS3 0xF5 packet: size = %d", data, size);
  178. #endif
  179. if (!ctx->is_shanwan) {
  180. // An output report could cause ShanWan controllers to rumble non-stop
  181. SDL_hid_write(device->dev, data, 1);
  182. }
  183. }
  184. device->type = SDL_GAMEPAD_TYPE_PS3;
  185. HIDAPI_SetDeviceName(device, "PS3 Controller");
  186. return HIDAPI_JoystickConnected(device, NULL);
  187. }
  188. static int HIDAPI_DriverPS3_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  189. {
  190. return -1;
  191. }
  192. static bool HIDAPI_DriverPS3_UpdateEffects(SDL_HIDAPI_Device *device)
  193. {
  194. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  195. Uint8 effects[] = {
  196. 0x01, 0xff, 0x00, 0xff, 0x00,
  197. 0x00, 0x00, 0x00, 0x00, 0x00,
  198. 0xff, 0x27, 0x10, 0x00, 0x32,
  199. 0xff, 0x27, 0x10, 0x00, 0x32,
  200. 0xff, 0x27, 0x10, 0x00, 0x32,
  201. 0xff, 0x27, 0x10, 0x00, 0x32,
  202. 0x00, 0x00, 0x00, 0x00, 0x00
  203. };
  204. effects[2] = ctx->rumble_right ? 1 : 0;
  205. effects[4] = ctx->rumble_left;
  206. effects[9] = (0x01 << (1 + (ctx->player_index % 4)));
  207. return HIDAPI_DriverPS3_SendJoystickEffect(device, ctx->joystick, effects, sizeof(effects));
  208. }
  209. static void HIDAPI_DriverPS3_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  210. {
  211. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  212. if (!ctx) {
  213. return;
  214. }
  215. ctx->player_index = player_index;
  216. // This will set the new LED state based on the new player index
  217. HIDAPI_DriverPS3_UpdateEffects(device);
  218. }
  219. static bool HIDAPI_DriverPS3_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  220. {
  221. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  222. SDL_AssertJoysticksLocked();
  223. ctx->joystick = joystick;
  224. ctx->effects_updated = false;
  225. ctx->rumble_left = 0;
  226. ctx->rumble_right = 0;
  227. SDL_zeroa(ctx->last_state);
  228. // Initialize player index (needed for setting LEDs)
  229. ctx->player_index = SDL_GetJoystickPlayerIndex(joystick);
  230. // Initialize the joystick capabilities
  231. joystick->nbuttons = 11;
  232. joystick->naxes = 6;
  233. if (ctx->has_analog_buttons) {
  234. joystick->naxes += 10;
  235. }
  236. joystick->nhats = 1;
  237. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 100.0f);
  238. return true;
  239. }
  240. static bool HIDAPI_DriverPS3_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  241. {
  242. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  243. ctx->rumble_left = (low_frequency_rumble >> 8);
  244. ctx->rumble_right = (high_frequency_rumble >> 8);
  245. return HIDAPI_DriverPS3_UpdateEffects(device);
  246. }
  247. static bool HIDAPI_DriverPS3_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  248. {
  249. return SDL_Unsupported();
  250. }
  251. static Uint32 HIDAPI_DriverPS3_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  252. {
  253. return SDL_JOYSTICK_CAP_RUMBLE;
  254. }
  255. static bool HIDAPI_DriverPS3_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  256. {
  257. return SDL_Unsupported();
  258. }
  259. static bool HIDAPI_DriverPS3_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size)
  260. {
  261. Uint8 data[49];
  262. int report_size, offset;
  263. SDL_zeroa(data);
  264. data[0] = k_EPS3ReportIdEffects;
  265. report_size = sizeof(data);
  266. offset = 1;
  267. SDL_memcpy(&data[offset], effect, SDL_min((sizeof(data) - offset), (size_t)size));
  268. if (SDL_HIDAPI_SendRumble(device, data, report_size) != report_size) {
  269. return SDL_SetError("Couldn't send rumble packet");
  270. }
  271. return true;
  272. }
  273. static bool HIDAPI_DriverPS3_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  274. {
  275. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  276. ctx->report_sensors = enabled;
  277. return true;
  278. }
  279. static float HIDAPI_DriverPS3_ScaleAccel(Sint16 value)
  280. {
  281. // Accelerometer values are in big endian order
  282. value = SDL_Swap16BE(value);
  283. return ((float)(value - 511) / 113.0f) * SDL_STANDARD_GRAVITY;
  284. }
  285. static void HIDAPI_DriverPS3_HandleMiniStatePacket(SDL_Joystick *joystick, SDL_DriverPS3_Context *ctx, Uint8 *data, int size)
  286. {
  287. Sint16 axis;
  288. Uint64 timestamp = SDL_GetTicksNS();
  289. if (ctx->last_state[4] != data[4]) {
  290. Uint8 hat;
  291. switch (data[4] & 0x0f) {
  292. case 0:
  293. hat = SDL_HAT_UP;
  294. break;
  295. case 1:
  296. hat = SDL_HAT_RIGHTUP;
  297. break;
  298. case 2:
  299. hat = SDL_HAT_RIGHT;
  300. break;
  301. case 3:
  302. hat = SDL_HAT_RIGHTDOWN;
  303. break;
  304. case 4:
  305. hat = SDL_HAT_DOWN;
  306. break;
  307. case 5:
  308. hat = SDL_HAT_LEFTDOWN;
  309. break;
  310. case 6:
  311. hat = SDL_HAT_LEFT;
  312. break;
  313. case 7:
  314. hat = SDL_HAT_LEFTUP;
  315. break;
  316. default:
  317. hat = SDL_HAT_CENTERED;
  318. break;
  319. }
  320. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  321. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[4] & 0x10) != 0));
  322. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[4] & 0x20) != 0));
  323. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[4] & 0x40) != 0));
  324. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[4] & 0x80) != 0));
  325. }
  326. if (ctx->last_state[5] != data[5]) {
  327. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[5] & 0x01) != 0));
  328. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[5] & 0x02) != 0));
  329. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, (data[5] & 0x04) ? SDL_JOYSTICK_AXIS_MAX : SDL_JOYSTICK_AXIS_MIN);
  330. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, (data[5] & 0x08) ? SDL_JOYSTICK_AXIS_MAX : SDL_JOYSTICK_AXIS_MIN);
  331. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[5] & 0x10) != 0));
  332. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[5] & 0x20) != 0));
  333. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[5] & 0x40) != 0));
  334. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[5] & 0x80) != 0));
  335. }
  336. axis = ((int)data[2] * 257) - 32768;
  337. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  338. axis = ((int)data[3] * 257) - 32768;
  339. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  340. axis = ((int)data[0] * 257) - 32768;
  341. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  342. axis = ((int)data[1] * 257) - 32768;
  343. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  344. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  345. }
  346. static void HIDAPI_DriverPS3_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverPS3_Context *ctx, Uint8 *data, int size)
  347. {
  348. Sint16 axis;
  349. Uint64 timestamp = SDL_GetTicksNS();
  350. if (ctx->last_state[2] != data[2]) {
  351. Uint8 hat = 0;
  352. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[2] & 0x01) != 0));
  353. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[2] & 0x02) != 0));
  354. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[2] & 0x04) != 0));
  355. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[2] & 0x08) != 0));
  356. if (data[2] & 0x10) {
  357. hat |= SDL_HAT_UP;
  358. }
  359. if (data[2] & 0x20) {
  360. hat |= SDL_HAT_RIGHT;
  361. }
  362. if (data[2] & 0x40) {
  363. hat |= SDL_HAT_DOWN;
  364. }
  365. if (data[2] & 0x80) {
  366. hat |= SDL_HAT_LEFT;
  367. }
  368. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  369. }
  370. if (ctx->last_state[3] != data[3]) {
  371. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[3] & 0x04) != 0));
  372. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[3] & 0x08) != 0));
  373. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[3] & 0x10) != 0));
  374. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[3] & 0x20) != 0));
  375. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[3] & 0x40) != 0));
  376. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[3] & 0x80) != 0));
  377. }
  378. if (ctx->last_state[4] != data[4]) {
  379. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data[4] & 0x01) != 0));
  380. }
  381. axis = ((int)data[18] * 257) - 32768;
  382. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  383. axis = ((int)data[19] * 257) - 32768;
  384. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  385. axis = ((int)data[6] * 257) - 32768;
  386. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  387. axis = ((int)data[7] * 257) - 32768;
  388. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  389. axis = ((int)data[8] * 257) - 32768;
  390. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  391. axis = ((int)data[9] * 257) - 32768;
  392. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  393. // Buttons are mapped as axes in the order they appear in the button enumeration
  394. if (ctx->has_analog_buttons) {
  395. static int button_axis_offsets[] = {
  396. 24, // SDL_GAMEPAD_BUTTON_SOUTH
  397. 23, // SDL_GAMEPAD_BUTTON_EAST
  398. 25, // SDL_GAMEPAD_BUTTON_WEST
  399. 22, // SDL_GAMEPAD_BUTTON_NORTH
  400. 0, // SDL_GAMEPAD_BUTTON_BACK
  401. 0, // SDL_GAMEPAD_BUTTON_GUIDE
  402. 0, // SDL_GAMEPAD_BUTTON_START
  403. 0, // SDL_GAMEPAD_BUTTON_LEFT_STICK
  404. 0, // SDL_GAMEPAD_BUTTON_RIGHT_STICK
  405. 20, // SDL_GAMEPAD_BUTTON_LEFT_SHOULDER
  406. 21, // SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER
  407. 14, // SDL_GAMEPAD_BUTTON_DPAD_UP
  408. 16, // SDL_GAMEPAD_BUTTON_DPAD_DOWN
  409. 17, // SDL_GAMEPAD_BUTTON_DPAD_LEFT
  410. 15, // SDL_GAMEPAD_BUTTON_DPAD_RIGHT
  411. };
  412. Uint8 i, axis_index = 6;
  413. for (i = 0; i < SDL_arraysize(button_axis_offsets); ++i) {
  414. int offset = button_axis_offsets[i];
  415. if (!offset) {
  416. // This button doesn't report as an axis
  417. continue;
  418. }
  419. axis = ((int)data[offset] * 257) - 32768;
  420. SDL_SendJoystickAxis(timestamp, joystick, axis_index, axis);
  421. ++axis_index;
  422. }
  423. }
  424. if (ctx->report_sensors) {
  425. float sensor_data[3];
  426. sensor_data[0] = HIDAPI_DriverPS3_ScaleAccel(LOAD16(data[41], data[42]));
  427. sensor_data[1] = -HIDAPI_DriverPS3_ScaleAccel(LOAD16(data[45], data[46]));
  428. sensor_data[2] = -HIDAPI_DriverPS3_ScaleAccel(LOAD16(data[43], data[44]));
  429. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, timestamp, sensor_data, SDL_arraysize(sensor_data));
  430. }
  431. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  432. }
  433. static bool HIDAPI_DriverPS3_UpdateDevice(SDL_HIDAPI_Device *device)
  434. {
  435. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  436. SDL_Joystick *joystick = NULL;
  437. Uint8 data[USB_PACKET_LENGTH];
  438. int size;
  439. if (device->num_joysticks > 0) {
  440. joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  441. } else {
  442. return false;
  443. }
  444. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  445. #ifdef DEBUG_PS3_PROTOCOL
  446. HIDAPI_DumpPacket("PS3 packet: size = %d", data, size);
  447. #endif
  448. if (!joystick) {
  449. continue;
  450. }
  451. if (size == 7) {
  452. // Seen on a ShanWan PS2 -> PS3 USB converter
  453. HIDAPI_DriverPS3_HandleMiniStatePacket(joystick, ctx, data, size);
  454. // Wait for the first report to set the LED state after the controller stops blinking
  455. if (!ctx->effects_updated) {
  456. HIDAPI_DriverPS3_UpdateEffects(device);
  457. ctx->effects_updated = true;
  458. }
  459. continue;
  460. }
  461. switch (data[0]) {
  462. case k_EPS3ReportIdState:
  463. if (data[1] == 0xFF) {
  464. // Invalid data packet, ignore
  465. break;
  466. }
  467. HIDAPI_DriverPS3_HandleStatePacket(joystick, ctx, data, size);
  468. // Wait for the first report to set the LED state after the controller stops blinking
  469. if (!ctx->effects_updated) {
  470. HIDAPI_DriverPS3_UpdateEffects(device);
  471. ctx->effects_updated = true;
  472. }
  473. break;
  474. default:
  475. #ifdef DEBUG_JOYSTICK
  476. SDL_Log("Unknown PS3 packet: 0x%.2x", data[0]);
  477. #endif
  478. break;
  479. }
  480. }
  481. if (size < 0) {
  482. // Read error, device is disconnected
  483. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  484. }
  485. return (size >= 0);
  486. }
  487. static void HIDAPI_DriverPS3_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  488. {
  489. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  490. ctx->joystick = NULL;
  491. }
  492. static void HIDAPI_DriverPS3_FreeDevice(SDL_HIDAPI_Device *device)
  493. {
  494. }
  495. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS3 = {
  496. SDL_HINT_JOYSTICK_HIDAPI_PS3,
  497. true,
  498. HIDAPI_DriverPS3_RegisterHints,
  499. HIDAPI_DriverPS3_UnregisterHints,
  500. HIDAPI_DriverPS3_IsEnabled,
  501. HIDAPI_DriverPS3_IsSupportedDevice,
  502. HIDAPI_DriverPS3_InitDevice,
  503. HIDAPI_DriverPS3_GetDevicePlayerIndex,
  504. HIDAPI_DriverPS3_SetDevicePlayerIndex,
  505. HIDAPI_DriverPS3_UpdateDevice,
  506. HIDAPI_DriverPS3_OpenJoystick,
  507. HIDAPI_DriverPS3_RumbleJoystick,
  508. HIDAPI_DriverPS3_RumbleJoystickTriggers,
  509. HIDAPI_DriverPS3_GetJoystickCapabilities,
  510. HIDAPI_DriverPS3_SetJoystickLED,
  511. HIDAPI_DriverPS3_SendJoystickEffect,
  512. HIDAPI_DriverPS3_SetJoystickSensorsEnabled,
  513. HIDAPI_DriverPS3_CloseJoystick,
  514. HIDAPI_DriverPS3_FreeDevice,
  515. };
  516. static bool HIDAPI_DriverPS3ThirdParty_IsEnabled(void)
  517. {
  518. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS3,
  519. SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI,
  520. SDL_HIDAPI_DEFAULT));
  521. }
  522. static bool HIDAPI_DriverPS3ThirdParty_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)
  523. {
  524. Uint8 data[USB_PACKET_LENGTH];
  525. int size;
  526. if (vendor_id == USB_VENDOR_LOGITECH &&
  527. product_id == USB_PRODUCT_LOGITECH_CHILLSTREAM) {
  528. return true;
  529. }
  530. if ((type == SDL_GAMEPAD_TYPE_PS3 && vendor_id != USB_VENDOR_SONY) ||
  531. HIDAPI_SupportsPlaystationDetection(vendor_id, product_id)) {
  532. if (device && device->dev) {
  533. size = ReadFeatureReport(device->dev, 0x03, data, sizeof(data));
  534. if (size == 8 && data[2] == 0x26) {
  535. // Supported third party controller
  536. return true;
  537. } else {
  538. return false;
  539. }
  540. } else {
  541. // Might be supported by this driver, enumerate and find out
  542. return true;
  543. }
  544. }
  545. return false;
  546. }
  547. static bool HIDAPI_DriverPS3ThirdParty_InitDevice(SDL_HIDAPI_Device *device)
  548. {
  549. SDL_DriverPS3_Context *ctx;
  550. ctx = (SDL_DriverPS3_Context *)SDL_calloc(1, sizeof(*ctx));
  551. if (!ctx) {
  552. return false;
  553. }
  554. ctx->device = device;
  555. if (device->vendor_id == USB_VENDOR_SWITCH && device->product_id == USB_PRODUCT_SWITCH_RETROBIT_CONTROLLER) {
  556. ctx->has_analog_buttons = false;
  557. } else {
  558. ctx->has_analog_buttons = true;
  559. }
  560. device->context = ctx;
  561. device->type = SDL_GAMEPAD_TYPE_PS3;
  562. if (device->vendor_id == USB_VENDOR_LOGITECH &&
  563. device->product_id == USB_PRODUCT_LOGITECH_CHILLSTREAM) {
  564. HIDAPI_SetDeviceName(device, "Logitech ChillStream");
  565. }
  566. return HIDAPI_JoystickConnected(device, NULL);
  567. }
  568. static int HIDAPI_DriverPS3ThirdParty_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  569. {
  570. return -1;
  571. }
  572. static void HIDAPI_DriverPS3ThirdParty_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  573. {
  574. }
  575. static bool HIDAPI_DriverPS3ThirdParty_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  576. {
  577. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  578. SDL_AssertJoysticksLocked();
  579. ctx->joystick = joystick;
  580. SDL_zeroa(ctx->last_state);
  581. // Initialize the joystick capabilities
  582. joystick->nbuttons = 11;
  583. joystick->naxes = 6;
  584. if (ctx->has_analog_buttons) {
  585. joystick->naxes += 10;
  586. }
  587. joystick->nhats = 1;
  588. if (device->vendor_id == USB_VENDOR_SWITCH && device->product_id == USB_PRODUCT_SWITCH_RETROBIT_CONTROLLER) {
  589. // This is a wireless controller using a USB dongle
  590. joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
  591. }
  592. return true;
  593. }
  594. static bool HIDAPI_DriverPS3ThirdParty_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  595. {
  596. return SDL_Unsupported();
  597. }
  598. static bool HIDAPI_DriverPS3ThirdParty_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  599. {
  600. return SDL_Unsupported();
  601. }
  602. static Uint32 HIDAPI_DriverPS3ThirdParty_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  603. {
  604. return 0;
  605. }
  606. static bool HIDAPI_DriverPS3ThirdParty_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  607. {
  608. return SDL_Unsupported();
  609. }
  610. static bool HIDAPI_DriverPS3ThirdParty_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size)
  611. {
  612. return SDL_Unsupported();
  613. }
  614. static bool HIDAPI_DriverPS3ThirdParty_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  615. {
  616. return SDL_Unsupported();
  617. }
  618. static void HIDAPI_DriverPS3ThirdParty_HandleStatePacket18(SDL_Joystick *joystick, SDL_DriverPS3_Context *ctx, Uint8 *data, int size)
  619. {
  620. Sint16 axis;
  621. Uint64 timestamp = SDL_GetTicksNS();
  622. if (ctx->last_state[0] != data[0]) {
  623. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[0] & 0x01) != 0));
  624. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[0] & 0x02) != 0));
  625. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[0] & 0x04) != 0));
  626. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[0] & 0x08) != 0));
  627. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[0] & 0x10) != 0));
  628. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[0] & 0x20) != 0));
  629. }
  630. if (ctx->last_state[1] != data[1]) {
  631. Uint8 hat;
  632. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[1] & 0x01) != 0));
  633. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[1] & 0x02) != 0));
  634. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[1] & 0x04) != 0));
  635. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[1] & 0x08) != 0));
  636. switch (data[1] >> 4) {
  637. case 0:
  638. hat = SDL_HAT_UP;
  639. break;
  640. case 1:
  641. hat = SDL_HAT_RIGHTUP;
  642. break;
  643. case 2:
  644. hat = SDL_HAT_RIGHT;
  645. break;
  646. case 3:
  647. hat = SDL_HAT_RIGHTDOWN;
  648. break;
  649. case 4:
  650. hat = SDL_HAT_DOWN;
  651. break;
  652. case 5:
  653. hat = SDL_HAT_LEFTDOWN;
  654. break;
  655. case 6:
  656. hat = SDL_HAT_LEFT;
  657. break;
  658. case 7:
  659. hat = SDL_HAT_LEFTUP;
  660. break;
  661. default:
  662. hat = SDL_HAT_CENTERED;
  663. break;
  664. }
  665. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  666. }
  667. axis = ((int)data[16] * 257) - 32768;
  668. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  669. axis = ((int)data[17] * 257) - 32768;
  670. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  671. axis = ((int)data[2] * 257) - 32768;
  672. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  673. axis = ((int)data[3] * 257) - 32768;
  674. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  675. axis = ((int)data[4] * 257) - 32768;
  676. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  677. axis = ((int)data[5] * 257) - 32768;
  678. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  679. // Buttons are mapped as axes in the order they appear in the button enumeration
  680. if (ctx->has_analog_buttons) {
  681. static int button_axis_offsets[] = {
  682. 12, // SDL_GAMEPAD_BUTTON_SOUTH
  683. 11, // SDL_GAMEPAD_BUTTON_EAST
  684. 13, // SDL_GAMEPAD_BUTTON_WEST
  685. 10, // SDL_GAMEPAD_BUTTON_NORTH
  686. 0, // SDL_GAMEPAD_BUTTON_BACK
  687. 0, // SDL_GAMEPAD_BUTTON_GUIDE
  688. 0, // SDL_GAMEPAD_BUTTON_START
  689. 0, // SDL_GAMEPAD_BUTTON_LEFT_STICK
  690. 0, // SDL_GAMEPAD_BUTTON_RIGHT_STICK
  691. 14, // SDL_GAMEPAD_BUTTON_LEFT_SHOULDER
  692. 15, // SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER
  693. 8, // SDL_GAMEPAD_BUTTON_DPAD_UP
  694. 9, // SDL_GAMEPAD_BUTTON_DPAD_DOWN
  695. 7, // SDL_GAMEPAD_BUTTON_DPAD_LEFT
  696. 6, // SDL_GAMEPAD_BUTTON_DPAD_RIGHT
  697. };
  698. Uint8 i, axis_index = 6;
  699. for (i = 0; i < SDL_arraysize(button_axis_offsets); ++i) {
  700. int offset = button_axis_offsets[i];
  701. if (!offset) {
  702. // This button doesn't report as an axis
  703. continue;
  704. }
  705. axis = ((int)data[offset] * 257) - 32768;
  706. SDL_SendJoystickAxis(timestamp, joystick, axis_index, axis);
  707. ++axis_index;
  708. }
  709. }
  710. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  711. }
  712. static void HIDAPI_DriverPS3ThirdParty_HandleStatePacket19(SDL_Joystick *joystick, SDL_DriverPS3_Context *ctx, Uint8 *data, int size)
  713. {
  714. Sint16 axis;
  715. Uint64 timestamp = SDL_GetTicksNS();
  716. if (ctx->last_state[0] != data[0]) {
  717. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[0] & 0x01) != 0));
  718. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[0] & 0x02) != 0));
  719. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[0] & 0x04) != 0));
  720. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[0] & 0x08) != 0));
  721. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[0] & 0x10) != 0));
  722. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[0] & 0x20) != 0));
  723. }
  724. if (ctx->last_state[1] != data[1]) {
  725. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[1] & 0x01) != 0));
  726. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[1] & 0x02) != 0));
  727. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[1] & 0x04) != 0));
  728. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[1] & 0x08) != 0));
  729. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data[1] & 0x10) != 0));
  730. }
  731. if (ctx->device->vendor_id == USB_VENDOR_SAITEK && ctx->device->product_id == USB_PRODUCT_SAITEK_CYBORG_V3) {
  732. // Cyborg V.3 Rumble Pad doesn't set the dpad bits as expected, so use the axes instead
  733. Uint8 hat = 0;
  734. if (data[7]) {
  735. hat |= SDL_HAT_RIGHT;
  736. }
  737. if (data[8]) {
  738. hat |= SDL_HAT_LEFT;
  739. }
  740. if (data[9]) {
  741. hat |= SDL_HAT_UP;
  742. }
  743. if (data[10]) {
  744. hat |= SDL_HAT_DOWN;
  745. }
  746. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  747. } else {
  748. if (ctx->last_state[2] != data[2]) {
  749. Uint8 hat;
  750. switch (data[2] & 0x0f) {
  751. case 0:
  752. hat = SDL_HAT_UP;
  753. break;
  754. case 1:
  755. hat = SDL_HAT_RIGHTUP;
  756. break;
  757. case 2:
  758. hat = SDL_HAT_RIGHT;
  759. break;
  760. case 3:
  761. hat = SDL_HAT_RIGHTDOWN;
  762. break;
  763. case 4:
  764. hat = SDL_HAT_DOWN;
  765. break;
  766. case 5:
  767. hat = SDL_HAT_LEFTDOWN;
  768. break;
  769. case 6:
  770. hat = SDL_HAT_LEFT;
  771. break;
  772. case 7:
  773. hat = SDL_HAT_LEFTUP;
  774. break;
  775. default:
  776. hat = SDL_HAT_CENTERED;
  777. break;
  778. }
  779. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  780. }
  781. }
  782. if (data[0] & 0x40) {
  783. axis = 32767;
  784. } else {
  785. axis = ((int)data[17] * 257) - 32768;
  786. }
  787. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  788. if (data[0] & 0x80) {
  789. axis = 32767;
  790. } else {
  791. axis = ((int)data[18] * 257) - 32768;
  792. }
  793. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  794. axis = ((int)data[3] * 257) - 32768;
  795. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  796. axis = ((int)data[4] * 257) - 32768;
  797. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  798. axis = ((int)data[5] * 257) - 32768;
  799. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  800. axis = ((int)data[6] * 257) - 32768;
  801. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  802. // Buttons are mapped as axes in the order they appear in the button enumeration
  803. if (ctx->has_analog_buttons) {
  804. static int button_axis_offsets[] = {
  805. 13, // SDL_GAMEPAD_BUTTON_SOUTH
  806. 12, // SDL_GAMEPAD_BUTTON_EAST
  807. 14, // SDL_GAMEPAD_BUTTON_WEST
  808. 11, // SDL_GAMEPAD_BUTTON_NORTH
  809. 0, // SDL_GAMEPAD_BUTTON_BACK
  810. 0, // SDL_GAMEPAD_BUTTON_GUIDE
  811. 0, // SDL_GAMEPAD_BUTTON_START
  812. 0, // SDL_GAMEPAD_BUTTON_LEFT_STICK
  813. 0, // SDL_GAMEPAD_BUTTON_RIGHT_STICK
  814. 15, // SDL_GAMEPAD_BUTTON_LEFT_SHOULDER
  815. 16, // SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER
  816. 9, // SDL_GAMEPAD_BUTTON_DPAD_UP
  817. 10, // SDL_GAMEPAD_BUTTON_DPAD_DOWN
  818. 8, // SDL_GAMEPAD_BUTTON_DPAD_LEFT
  819. 7, // SDL_GAMEPAD_BUTTON_DPAD_RIGHT
  820. };
  821. Uint8 i, axis_index = 6;
  822. for (i = 0; i < SDL_arraysize(button_axis_offsets); ++i) {
  823. int offset = button_axis_offsets[i];
  824. if (!offset) {
  825. // This button doesn't report as an axis
  826. continue;
  827. }
  828. axis = ((int)data[offset] * 257) - 32768;
  829. SDL_SendJoystickAxis(timestamp, joystick, axis_index, axis);
  830. ++axis_index;
  831. }
  832. }
  833. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  834. }
  835. static bool HIDAPI_DriverPS3ThirdParty_UpdateDevice(SDL_HIDAPI_Device *device)
  836. {
  837. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  838. SDL_Joystick *joystick = NULL;
  839. Uint8 data[USB_PACKET_LENGTH];
  840. int size;
  841. if (device->num_joysticks > 0) {
  842. joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  843. } else {
  844. return false;
  845. }
  846. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  847. #ifdef DEBUG_PS3_PROTOCOL
  848. HIDAPI_DumpPacket("PS3 packet: size = %d", data, size);
  849. #endif
  850. if (!joystick) {
  851. continue;
  852. }
  853. if (size >= 19) {
  854. HIDAPI_DriverPS3ThirdParty_HandleStatePacket19(joystick, ctx, data, size);
  855. } else if (size == 18) {
  856. // This packet format was seen with the Logitech ChillStream
  857. HIDAPI_DriverPS3ThirdParty_HandleStatePacket18(joystick, ctx, data, size);
  858. } else {
  859. #ifdef DEBUG_JOYSTICK
  860. SDL_Log("Unknown PS3 packet, size %d", size);
  861. #endif
  862. }
  863. }
  864. if (size < 0) {
  865. // Read error, device is disconnected
  866. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  867. }
  868. return (size >= 0);
  869. }
  870. static void HIDAPI_DriverPS3ThirdParty_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  871. {
  872. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  873. ctx->joystick = NULL;
  874. }
  875. static void HIDAPI_DriverPS3ThirdParty_FreeDevice(SDL_HIDAPI_Device *device)
  876. {
  877. }
  878. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS3ThirdParty = {
  879. SDL_HINT_JOYSTICK_HIDAPI_PS3,
  880. true,
  881. HIDAPI_DriverPS3_RegisterHints,
  882. HIDAPI_DriverPS3_UnregisterHints,
  883. HIDAPI_DriverPS3ThirdParty_IsEnabled,
  884. HIDAPI_DriverPS3ThirdParty_IsSupportedDevice,
  885. HIDAPI_DriverPS3ThirdParty_InitDevice,
  886. HIDAPI_DriverPS3ThirdParty_GetDevicePlayerIndex,
  887. HIDAPI_DriverPS3ThirdParty_SetDevicePlayerIndex,
  888. HIDAPI_DriverPS3ThirdParty_UpdateDevice,
  889. HIDAPI_DriverPS3ThirdParty_OpenJoystick,
  890. HIDAPI_DriverPS3ThirdParty_RumbleJoystick,
  891. HIDAPI_DriverPS3ThirdParty_RumbleJoystickTriggers,
  892. HIDAPI_DriverPS3ThirdParty_GetJoystickCapabilities,
  893. HIDAPI_DriverPS3ThirdParty_SetJoystickLED,
  894. HIDAPI_DriverPS3ThirdParty_SendJoystickEffect,
  895. HIDAPI_DriverPS3ThirdParty_SetJoystickSensorsEnabled,
  896. HIDAPI_DriverPS3ThirdParty_CloseJoystick,
  897. HIDAPI_DriverPS3ThirdParty_FreeDevice,
  898. };
  899. static bool HIDAPI_DriverPS3_UpdateRumbleSonySixaxis(SDL_HIDAPI_Device *device);
  900. static bool HIDAPI_DriverPS3_UpdateLEDsSonySixaxis(SDL_HIDAPI_Device *device);
  901. static void HIDAPI_DriverPS3SonySixaxis_RegisterHints(SDL_HintCallback callback, void *userdata)
  902. {
  903. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS3_SIXAXIS_DRIVER, callback, userdata);
  904. }
  905. static void HIDAPI_DriverPS3SonySixaxis_UnregisterHints(SDL_HintCallback callback, void *userdata)
  906. {
  907. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS3_SIXAXIS_DRIVER, callback, userdata);
  908. }
  909. static bool HIDAPI_DriverPS3SonySixaxis_IsEnabled(void)
  910. {
  911. #ifdef SDL_PLATFORM_WIN32
  912. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS3_SIXAXIS_DRIVER, false);
  913. #else
  914. return false;
  915. #endif
  916. }
  917. static bool HIDAPI_DriverPS3SonySixaxis_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)
  918. {
  919. if (vendor_id == USB_VENDOR_SONY && product_id == USB_PRODUCT_SONY_DS3) {
  920. return true;
  921. }
  922. return false;
  923. }
  924. static bool HIDAPI_DriverPS3SonySixaxis_InitDevice(SDL_HIDAPI_Device *device)
  925. {
  926. SDL_DriverPS3_Context *ctx;
  927. ctx = (SDL_DriverPS3_Context *)SDL_calloc(1, sizeof(*ctx));
  928. if (!ctx) {
  929. return false;
  930. }
  931. ctx->device = device;
  932. ctx->has_analog_buttons = true;
  933. device->context = ctx;
  934. Uint8 data[USB_PACKET_LENGTH];
  935. int size = ReadFeatureReport(device->dev, 0xf2, data, sizeof(data));
  936. if (size < 0) {
  937. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  938. "HIDAPI_DriverPS3SonySixaxis_InitDevice(): Couldn't read feature report 0xf2. Trying again with 0x0.");
  939. SDL_zeroa(data);
  940. size = ReadFeatureReport(device->dev, 0x00, data, sizeof(data));
  941. if (size < 0) {
  942. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  943. "HIDAPI_DriverPS3SonySixaxis_InitDevice(): Couldn't read feature report 0x00.");
  944. return false;
  945. }
  946. #ifdef DEBUG_PS3_PROTOCOL
  947. HIDAPI_DumpPacket("PS3 0x0 packet: size = %d", data, size);
  948. #endif
  949. }
  950. #ifdef DEBUG_PS3_PROTOCOL
  951. HIDAPI_DumpPacket("PS3 0xF2 packet: size = %d", data, size);
  952. #endif
  953. device->type = SDL_GAMEPAD_TYPE_PS3;
  954. HIDAPI_SetDeviceName(device, "PS3 Controller");
  955. return HIDAPI_JoystickConnected(device, NULL);
  956. }
  957. static int HIDAPI_DriverPS3SonySixaxis_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  958. {
  959. return -1;
  960. }
  961. static void HIDAPI_DriverPS3SonySixaxis_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  962. {
  963. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  964. if (!ctx) {
  965. return;
  966. }
  967. ctx->player_index = player_index;
  968. // This will set the new LED state based on the new player index
  969. HIDAPI_DriverPS3_UpdateLEDsSonySixaxis(device);
  970. }
  971. static bool HIDAPI_DriverPS3SonySixaxis_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  972. {
  973. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  974. SDL_AssertJoysticksLocked();
  975. ctx->joystick = joystick;
  976. ctx->effects_updated = false;
  977. ctx->rumble_left = 0;
  978. ctx->rumble_right = 0;
  979. SDL_zeroa(ctx->last_state);
  980. // Initialize player index (needed for setting LEDs)
  981. ctx->player_index = SDL_GetJoystickPlayerIndex(joystick);
  982. // Initialize the joystick capabilities
  983. joystick->nbuttons = 11;
  984. joystick->naxes = 6;
  985. if (ctx->has_analog_buttons) {
  986. joystick->naxes += 10;
  987. }
  988. joystick->nhats = 1;
  989. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 100.0f);
  990. return true;
  991. }
  992. static bool HIDAPI_DriverPS3SonySixaxis_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  993. {
  994. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  995. ctx->rumble_left = (low_frequency_rumble >> 8);
  996. ctx->rumble_right = (high_frequency_rumble >> 8);
  997. return HIDAPI_DriverPS3_UpdateRumbleSonySixaxis(device);
  998. }
  999. static bool HIDAPI_DriverPS3SonySixaxis_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  1000. {
  1001. return SDL_Unsupported();
  1002. }
  1003. static Uint32 HIDAPI_DriverPS3SonySixaxis_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  1004. {
  1005. return 0;
  1006. }
  1007. static bool HIDAPI_DriverPS3SonySixaxis_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  1008. {
  1009. return SDL_Unsupported();
  1010. }
  1011. static bool HIDAPI_DriverPS3SonySixaxis_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size)
  1012. {
  1013. Uint8 data[49];
  1014. int report_size;
  1015. SDL_zeroa(data);
  1016. data[0] = k_EPS3SonySixaxisReportIdEffects;
  1017. report_size = sizeof(data);
  1018. // No offset with Sony sixaxis.sys driver
  1019. SDL_memcpy(&data, effect, SDL_min(sizeof(data), (size_t)size));
  1020. if (SDL_HIDAPI_SendRumble(device, data, report_size) != report_size) {
  1021. return SDL_SetError("Couldn't send rumble packet");
  1022. }
  1023. return true;
  1024. }
  1025. static bool HIDAPI_DriverPS3SonySixaxis_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  1026. {
  1027. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  1028. ctx->report_sensors = enabled;
  1029. return true;
  1030. }
  1031. static void HIDAPI_DriverPS3SonySixaxis_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverPS3_Context *ctx, Uint8 *data, int size)
  1032. {
  1033. Sint16 axis;
  1034. Uint64 timestamp = SDL_GetTicksNS();
  1035. if (ctx->last_state[2] != data[2]) {
  1036. Uint8 hat = 0;
  1037. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[2] & 0x01) != 0));
  1038. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[2] & 0x02) != 0));
  1039. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[2] & 0x04) != 0));
  1040. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[2] & 0x08) != 0));
  1041. if (data[2] & 0x10) {
  1042. hat |= SDL_HAT_UP;
  1043. }
  1044. if (data[2] & 0x20) {
  1045. hat |= SDL_HAT_RIGHT;
  1046. }
  1047. if (data[2] & 0x40) {
  1048. hat |= SDL_HAT_DOWN;
  1049. }
  1050. if (data[2] & 0x80) {
  1051. hat |= SDL_HAT_LEFT;
  1052. }
  1053. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  1054. }
  1055. if (ctx->last_state[3] != data[3]) {
  1056. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[3] & 0x04) != 0));
  1057. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[3] & 0x08) != 0));
  1058. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[3] & 0x10) != 0));
  1059. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[3] & 0x20) != 0));
  1060. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[3] & 0x40) != 0));
  1061. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[3] & 0x80) != 0));
  1062. }
  1063. if (ctx->last_state[4] != data[4]) {
  1064. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data[4] & 0x01) != 0));
  1065. }
  1066. axis = ((int)data[18] * 257) - 32768;
  1067. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  1068. axis = ((int)data[19] * 257) - 32768;
  1069. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  1070. axis = ((int)data[6] * 257) - 32768;
  1071. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  1072. axis = ((int)data[7] * 257) - 32768;
  1073. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  1074. axis = ((int)data[8] * 257) - 32768;
  1075. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  1076. axis = ((int)data[9] * 257) - 32768;
  1077. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  1078. // Buttons are mapped as axes in the order they appear in the button enumeration
  1079. if (ctx->has_analog_buttons) {
  1080. static int button_axis_offsets[] = {
  1081. 24, // SDL_GAMEPAD_BUTTON_SOUTH
  1082. 23, // SDL_GAMEPAD_BUTTON_EAST
  1083. 25, // SDL_GAMEPAD_BUTTON_WEST
  1084. 22, // SDL_GAMEPAD_BUTTON_NORTH
  1085. 0, // SDL_GAMEPAD_BUTTON_BACK
  1086. 0, // SDL_GAMEPAD_BUTTON_GUIDE
  1087. 0, // SDL_GAMEPAD_BUTTON_START
  1088. 0, // SDL_GAMEPAD_BUTTON_LEFT_STICK
  1089. 0, // SDL_GAMEPAD_BUTTON_RIGHT_STICK
  1090. 20, // SDL_GAMEPAD_BUTTON_LEFT_SHOULDER
  1091. 21, // SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER
  1092. 14, // SDL_GAMEPAD_BUTTON_DPAD_UP
  1093. 16, // SDL_GAMEPAD_BUTTON_DPAD_DOWN
  1094. 17, // SDL_GAMEPAD_BUTTON_DPAD_LEFT
  1095. 15, // SDL_GAMEPAD_BUTTON_DPAD_RIGHT
  1096. };
  1097. Uint8 i, axis_index = 6;
  1098. for (i = 0; i < SDL_arraysize(button_axis_offsets); ++i) {
  1099. int offset = button_axis_offsets[i];
  1100. if (!offset) {
  1101. // This button doesn't report as an axis
  1102. continue;
  1103. }
  1104. axis = ((int)data[offset] * 257) - 32768;
  1105. SDL_SendJoystickAxis(timestamp, joystick, axis_index, axis);
  1106. ++axis_index;
  1107. }
  1108. }
  1109. if (ctx->report_sensors) {
  1110. float sensor_data[3];
  1111. sensor_data[0] = HIDAPI_DriverPS3_ScaleAccel(LOAD16(data[41], data[42]));
  1112. sensor_data[1] = -HIDAPI_DriverPS3_ScaleAccel(LOAD16(data[45], data[46]));
  1113. sensor_data[2] = -HIDAPI_DriverPS3_ScaleAccel(LOAD16(data[43], data[44]));
  1114. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, timestamp, sensor_data, SDL_arraysize(sensor_data));
  1115. }
  1116. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  1117. }
  1118. static bool HIDAPI_DriverPS3SonySixaxis_UpdateDevice(SDL_HIDAPI_Device *device)
  1119. {
  1120. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  1121. SDL_Joystick *joystick = NULL;
  1122. Uint8 data[USB_PACKET_LENGTH];
  1123. int size;
  1124. if (device->num_joysticks > 0) {
  1125. joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  1126. } else {
  1127. return false;
  1128. }
  1129. if (!joystick) {
  1130. return false;
  1131. }
  1132. // With sixaxis.sys driver we need to use hid_get_feature_report instead of hid_read
  1133. size = ReadFeatureReport(device->dev, 0x0, data, sizeof(data));
  1134. if (size < 0) {
  1135. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  1136. "HIDAPI_DriverPS3SonySixaxis_UpdateDevice(): Couldn't read feature report 0x00");
  1137. return false;
  1138. }
  1139. switch (data[0]) {
  1140. case k_EPS3SonySixaxisReportIdState:
  1141. HIDAPI_DriverPS3SonySixaxis_HandleStatePacket(joystick, ctx, &data[1], size - 1); // report data starts in data[1]
  1142. // Wait for the first report to set the LED state after the controller stops blinking
  1143. if (!ctx->effects_updated) {
  1144. HIDAPI_DriverPS3_UpdateLEDsSonySixaxis(device);
  1145. ctx->effects_updated = true;
  1146. }
  1147. break;
  1148. default:
  1149. #ifdef DEBUG_JOYSTICK
  1150. SDL_Log("Unknown PS3 packet: 0x%.2x", data[0]);
  1151. #endif
  1152. break;
  1153. }
  1154. if (size < 0) {
  1155. // Read error, device is disconnected
  1156. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  1157. }
  1158. return (size >= 0);
  1159. }
  1160. static void HIDAPI_DriverPS3SonySixaxis_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  1161. {
  1162. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  1163. ctx->joystick = NULL;
  1164. }
  1165. static void HIDAPI_DriverPS3SonySixaxis_FreeDevice(SDL_HIDAPI_Device *device)
  1166. {
  1167. }
  1168. static bool HIDAPI_DriverPS3_UpdateRumbleSonySixaxis(SDL_HIDAPI_Device *device)
  1169. {
  1170. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  1171. Uint8 effects[] = {
  1172. 0x0, // Report Id
  1173. k_EPS3SixaxisCommandSetMotors, // 2 = Set Motors
  1174. 0x00, 0x00, 0x00, // padding
  1175. 0xff, // Small Motor duration - 0xff is forever
  1176. 0x00, // Small Motor off/on (0 or 1)
  1177. 0xff, // Large Motor duration - 0xff is forever
  1178. 0x00 // Large Motor force (0 to 255)
  1179. };
  1180. effects[6] = ctx->rumble_right ? 1 : 0; // Small motor
  1181. effects[8] = ctx->rumble_left; // Large motor
  1182. return HIDAPI_DriverPS3SonySixaxis_SendJoystickEffect(device, ctx->joystick, effects, sizeof(effects));
  1183. }
  1184. static bool HIDAPI_DriverPS3_UpdateLEDsSonySixaxis(SDL_HIDAPI_Device *device)
  1185. {
  1186. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  1187. Uint8 effects[] = {
  1188. 0x0, // Report Id
  1189. k_EPS3SixaxisCommandSetLEDs, // 1 = Set LEDs
  1190. 0x00, 0x00, 0x00, // padding
  1191. 0x00, 0x00, 0x00, 0x00 // LED #4, LED #3, LED #2, LED #1 (0 = Off, 1 = On, 2 = Flashing)
  1192. };
  1193. // Turn on LED light on DS3 Controller for relevant player (player_index 0 lights up LED #1, player_index 1 lights up LED #2, etc)
  1194. if (ctx->player_index < 4) {
  1195. effects[8 - ctx->player_index] = 1;
  1196. }
  1197. return HIDAPI_DriverPS3SonySixaxis_SendJoystickEffect(device, ctx->joystick, effects, sizeof(effects));
  1198. }
  1199. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS3SonySixaxis = {
  1200. SDL_HINT_JOYSTICK_HIDAPI_PS3_SIXAXIS_DRIVER,
  1201. true,
  1202. HIDAPI_DriverPS3SonySixaxis_RegisterHints,
  1203. HIDAPI_DriverPS3SonySixaxis_UnregisterHints,
  1204. HIDAPI_DriverPS3SonySixaxis_IsEnabled,
  1205. HIDAPI_DriverPS3SonySixaxis_IsSupportedDevice,
  1206. HIDAPI_DriverPS3SonySixaxis_InitDevice,
  1207. HIDAPI_DriverPS3SonySixaxis_GetDevicePlayerIndex,
  1208. HIDAPI_DriverPS3SonySixaxis_SetDevicePlayerIndex,
  1209. HIDAPI_DriverPS3SonySixaxis_UpdateDevice,
  1210. HIDAPI_DriverPS3SonySixaxis_OpenJoystick,
  1211. HIDAPI_DriverPS3SonySixaxis_RumbleJoystick,
  1212. HIDAPI_DriverPS3SonySixaxis_RumbleJoystickTriggers,
  1213. HIDAPI_DriverPS3SonySixaxis_GetJoystickCapabilities,
  1214. HIDAPI_DriverPS3SonySixaxis_SetJoystickLED,
  1215. HIDAPI_DriverPS3SonySixaxis_SendJoystickEffect,
  1216. HIDAPI_DriverPS3SonySixaxis_SetJoystickSensorsEnabled,
  1217. HIDAPI_DriverPS3SonySixaxis_CloseJoystick,
  1218. HIDAPI_DriverPS3SonySixaxis_FreeDevice,
  1219. };
  1220. #endif // SDL_JOYSTICK_HIDAPI_PS3
  1221. #endif // SDL_JOYSTICK_HIDAPI