SDL_hidapi_ps3.c 52 KB

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