SDL_hidapi_ps3.c 54 KB

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