1
0

SDL_hidapi_ps3.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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.h"
  21. #include "SDL_events.h"
  22. #include "SDL_timer.h"
  23. #include "SDL_joystick.h"
  24. #include "SDL_gamecontroller.h"
  25. #include "../../SDL_hints_c.h"
  26. #include "../SDL_sysjoystick.h"
  27. #include "SDL_hidapijoystick_c.h"
  28. #include "SDL_hidapi_rumble.h"
  29. #ifdef SDL_JOYSTICK_HIDAPI_PS3
  30. /* Define this if you want to log all packets from the controller */
  31. /*#define DEBUG_PS3_PROTOCOL*/
  32. #define LOAD16(A, B) (Sint16)((Uint16)(A) | (((Uint16)(B)) << 8))
  33. typedef enum
  34. {
  35. k_EPS3ReportIdState = 1,
  36. k_EPS3ReportIdEffects = 1,
  37. } EPS3ReportId;
  38. typedef struct {
  39. SDL_HIDAPI_Device *device;
  40. SDL_Joystick *joystick;
  41. SDL_bool is_shanwan;
  42. SDL_bool report_sensors;
  43. SDL_bool effects_updated;
  44. int player_index;
  45. Uint8 rumble_left;
  46. Uint8 rumble_right;
  47. Uint8 last_state[USB_PACKET_LENGTH];
  48. } SDL_DriverPS3_Context;
  49. static int HIDAPI_DriverPS3_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size);
  50. static void
  51. HIDAPI_DriverPS3_RegisterHints(SDL_HintCallback callback, void *userdata)
  52. {
  53. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS3, callback, userdata);
  54. }
  55. static void
  56. HIDAPI_DriverPS3_UnregisterHints(SDL_HintCallback callback, void *userdata)
  57. {
  58. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS3, callback, userdata);
  59. }
  60. static SDL_bool
  61. HIDAPI_DriverPS3_IsEnabled(void)
  62. {
  63. SDL_bool default_value;
  64. #if defined(__MACOSX__)
  65. /* This works well on macOS */
  66. default_value = SDL_TRUE;
  67. #elif defined(__WINDOWS__)
  68. /* You can't initialize the controller with the stock Windows drivers
  69. * See https://github.com/ViGEm/DsHidMini as an alternative driver
  70. */
  71. default_value = SDL_FALSE;
  72. #elif defined(__LINUX__)
  73. /* Linux drivers do a better job of managing the transition between
  74. * USB and Bluetooth. There are also some quirks in communicating
  75. * with PS3 controllers that have been implemented in SDL's hidapi
  76. * for libusb, but are not possible to support using hidraw if the
  77. * kernel doesn't already know about them.
  78. */
  79. default_value = SDL_FALSE;
  80. #else
  81. /* Untested, default off */
  82. default_value = SDL_FALSE;
  83. #endif
  84. if (default_value) {
  85. default_value = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT);
  86. }
  87. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS3, default_value);
  88. }
  89. static SDL_bool
  90. HIDAPI_DriverPS3_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
  91. {
  92. if (vendor_id == USB_VENDOR_SONY && product_id == USB_PRODUCT_SONY_DS3) {
  93. return SDL_TRUE;
  94. }
  95. if (vendor_id == USB_VENDOR_SHANWAN && product_id == USB_PRODUCT_SHANWAN_DS3) {
  96. return SDL_TRUE;
  97. }
  98. return SDL_FALSE;
  99. }
  100. static int ReadFeatureReport(SDL_hid_device *dev, Uint8 report_id, Uint8 *report, size_t length)
  101. {
  102. SDL_memset(report, 0, length);
  103. report[0] = report_id;
  104. return SDL_hid_get_feature_report(dev, report, length);
  105. }
  106. static int SendFeatureReport(SDL_hid_device *dev, Uint8 *report, size_t length)
  107. {
  108. return SDL_hid_send_feature_report(dev, report, length);
  109. }
  110. static SDL_bool
  111. HIDAPI_DriverPS3_InitDevice(SDL_HIDAPI_Device *device)
  112. {
  113. SDL_DriverPS3_Context *ctx;
  114. SDL_bool is_shanwan = SDL_FALSE;
  115. if (device->vendor_id == USB_VENDOR_SONY &&
  116. SDL_strncasecmp(device->name, "ShanWan", 7) == 0) {
  117. is_shanwan = SDL_TRUE;
  118. }
  119. if (device->vendor_id == USB_VENDOR_SHANWAN ||
  120. device->vendor_id == USB_VENDOR_SHANWAN_ALT) {
  121. is_shanwan = SDL_TRUE;
  122. }
  123. ctx = (SDL_DriverPS3_Context *)SDL_calloc(1, sizeof(*ctx));
  124. if (!ctx) {
  125. SDL_OutOfMemory();
  126. return SDL_FALSE;
  127. }
  128. ctx->device = device;
  129. ctx->is_shanwan = is_shanwan;
  130. device->context = ctx;
  131. /* Set the controller into report mode over Bluetooth */
  132. {
  133. Uint8 data[] = { 0xf4, 0x42, 0x03, 0x00, 0x00 };
  134. SendFeatureReport(device->dev, data, sizeof(data));
  135. }
  136. /* Set the controller into report mode over USB */
  137. {
  138. Uint8 data[USB_PACKET_LENGTH];
  139. int size;
  140. if ((size = ReadFeatureReport(device->dev, 0xf2, data, 17)) < 0) {
  141. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  142. "HIDAPI_DriverPS3_InitDevice(): Couldn't read feature report 0xf2");
  143. return SDL_FALSE;
  144. }
  145. #ifdef DEBUG_PS3_PROTOCOL
  146. HIDAPI_DumpPacket("PS3 0xF2 packet: size = %d", data, size);
  147. #endif
  148. if ((size = ReadFeatureReport(device->dev, 0xf5, data, 8)) < 0) {
  149. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  150. "HIDAPI_DriverPS3_InitDevice(): Couldn't read feature report 0xf5");
  151. return SDL_FALSE;
  152. }
  153. #ifdef DEBUG_PS3_PROTOCOL
  154. HIDAPI_DumpPacket("PS3 0xF5 packet: size = %d", data, size);
  155. #endif
  156. if (!ctx->is_shanwan) {
  157. /* An output report could cause ShanWan controllers to rumble non-stop */
  158. SDL_hid_write(device->dev, data, 1);
  159. }
  160. }
  161. device->type = SDL_CONTROLLER_TYPE_PS3;
  162. HIDAPI_SetDeviceName(device, "PS3 Controller");
  163. return HIDAPI_JoystickConnected(device, NULL);
  164. }
  165. static int
  166. HIDAPI_DriverPS3_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  167. {
  168. return -1;
  169. }
  170. static int
  171. HIDAPI_DriverPS3_UpdateEffects(SDL_HIDAPI_Device *device)
  172. {
  173. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  174. Uint8 effects[] = {
  175. 0x01, 0xff, 0x00, 0xff, 0x00,
  176. 0x00, 0x00, 0x00, 0x00, 0x00,
  177. 0xff, 0x27, 0x10, 0x00, 0x32,
  178. 0xff, 0x27, 0x10, 0x00, 0x32,
  179. 0xff, 0x27, 0x10, 0x00, 0x32,
  180. 0xff, 0x27, 0x10, 0x00, 0x32,
  181. 0x00, 0x00, 0x00, 0x00, 0x00
  182. };
  183. effects[2] = ctx->rumble_right ? 1 : 0;
  184. effects[4] = ctx->rumble_left;
  185. effects[9] = (0x01 << (1+(ctx->player_index % 4)));
  186. return HIDAPI_DriverPS3_SendJoystickEffect(device, ctx->joystick, effects, sizeof(effects));
  187. }
  188. static void
  189. HIDAPI_DriverPS3_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  190. {
  191. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  192. if (!ctx) {
  193. return;
  194. }
  195. ctx->player_index = player_index;
  196. /* This will set the new LED state based on the new player index */
  197. HIDAPI_DriverPS3_UpdateEffects(device);
  198. }
  199. static SDL_bool
  200. HIDAPI_DriverPS3_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  201. {
  202. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  203. ctx->joystick = joystick;
  204. ctx->effects_updated = SDL_FALSE;
  205. ctx->rumble_left = 0;
  206. ctx->rumble_right = 0;
  207. SDL_zeroa(ctx->last_state);
  208. /* Initialize player index (needed for setting LEDs) */
  209. ctx->player_index = SDL_JoystickGetPlayerIndex(joystick);
  210. /* Initialize the joystick capabilities */
  211. joystick->nbuttons = 15;
  212. joystick->naxes = 16;
  213. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  214. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 100.0f);
  215. return SDL_TRUE;
  216. }
  217. static int
  218. HIDAPI_DriverPS3_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  219. {
  220. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  221. ctx->rumble_left = (low_frequency_rumble >> 8);
  222. ctx->rumble_right = (high_frequency_rumble >> 8);
  223. return HIDAPI_DriverPS3_UpdateEffects(device);
  224. }
  225. static int
  226. HIDAPI_DriverPS3_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  227. {
  228. return SDL_Unsupported();
  229. }
  230. static Uint32
  231. HIDAPI_DriverPS3_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  232. {
  233. return SDL_JOYCAP_RUMBLE;
  234. }
  235. static int
  236. HIDAPI_DriverPS3_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  237. {
  238. return SDL_Unsupported();
  239. }
  240. static int
  241. HIDAPI_DriverPS3_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size)
  242. {
  243. Uint8 data[49];
  244. int report_size, offset;
  245. SDL_zeroa(data);
  246. data[0] = k_EPS3ReportIdEffects;
  247. report_size = sizeof(data);
  248. offset = 1;
  249. SDL_memcpy(&data[offset], effect, SDL_min((sizeof(data) - offset), (size_t)size));
  250. if (SDL_HIDAPI_SendRumble(device, data, report_size) != report_size) {
  251. return SDL_SetError("Couldn't send rumble packet");
  252. }
  253. return 0;
  254. }
  255. static int
  256. HIDAPI_DriverPS3_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
  257. {
  258. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  259. ctx->report_sensors = enabled;
  260. return 0;
  261. }
  262. static float
  263. HIDAPI_DriverPS3_ScaleAccel(Sint16 value)
  264. {
  265. /* Accelerometer values are in big endian order */
  266. value = SDL_SwapBE16(value);
  267. return (float)(value - 511) / 113.0f;
  268. }
  269. static void
  270. HIDAPI_DriverPS3_HandleMiniStatePacket(SDL_Joystick *joystick, SDL_DriverPS3_Context *ctx, Uint8 *data, int size)
  271. {
  272. Sint16 axis;
  273. if (ctx->last_state[4] != data[4]) {
  274. SDL_bool dpad_up = SDL_FALSE;
  275. SDL_bool dpad_down = SDL_FALSE;
  276. SDL_bool dpad_left = SDL_FALSE;
  277. SDL_bool dpad_right = SDL_FALSE;
  278. switch (data[4] & 0x0f) {
  279. case 0:
  280. dpad_up = SDL_TRUE;
  281. break;
  282. case 1:
  283. dpad_up = SDL_TRUE;
  284. dpad_right = SDL_TRUE;
  285. break;
  286. case 2:
  287. dpad_right = SDL_TRUE;
  288. break;
  289. case 3:
  290. dpad_right = SDL_TRUE;
  291. dpad_down = SDL_TRUE;
  292. break;
  293. case 4:
  294. dpad_down = SDL_TRUE;
  295. break;
  296. case 5:
  297. dpad_left = SDL_TRUE;
  298. dpad_down = SDL_TRUE;
  299. break;
  300. case 6:
  301. dpad_left = SDL_TRUE;
  302. break;
  303. case 7:
  304. dpad_up = SDL_TRUE;
  305. dpad_left = SDL_TRUE;
  306. break;
  307. default:
  308. break;
  309. }
  310. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  311. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  312. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  313. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  314. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[4] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  315. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[4] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  316. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[4] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  317. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[4] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  318. }
  319. if (ctx->last_state[5] != data[5]) {
  320. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[5] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  321. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[5] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  322. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, (data[5] & 0x04) ? SDL_JOYSTICK_AXIS_MAX : SDL_JOYSTICK_AXIS_MIN);
  323. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, (data[5] & 0x08) ? SDL_JOYSTICK_AXIS_MAX : SDL_JOYSTICK_AXIS_MIN);
  324. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[5] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  325. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[5] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  326. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[5] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  327. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[5] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  328. }
  329. axis = ((int)data[2] * 257) - 32768;
  330. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  331. axis = ((int)data[3] * 257) - 32768;
  332. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  333. axis = ((int)data[0] * 257) - 32768;
  334. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  335. axis = ((int)data[1] * 257) - 32768;
  336. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  337. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  338. }
  339. static void
  340. HIDAPI_DriverPS3_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverPS3_Context *ctx, Uint8 *data, int size)
  341. {
  342. Sint16 axis;
  343. if (ctx->last_state[2] != data[2]) {
  344. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[2] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  345. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[2] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  346. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[2] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  347. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[2] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  348. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data[2] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  349. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data[2] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  350. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (data[2] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  351. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data[2] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  352. }
  353. if (ctx->last_state[3] != data[3]) {
  354. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[3] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  355. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[3] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  356. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[3] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  357. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[3] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  358. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[3] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  359. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[3] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  360. }
  361. if (ctx->last_state[4] != data[4]) {
  362. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[4] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  363. }
  364. axis = ((int)data[18] * 257) - 32768;
  365. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  366. axis = ((int)data[19] * 257) - 32768;
  367. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  368. axis = ((int)data[6] * 257) - 32768;
  369. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  370. axis = ((int)data[7] * 257) - 32768;
  371. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  372. axis = ((int)data[8] * 257) - 32768;
  373. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  374. axis = ((int)data[9] * 257) - 32768;
  375. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  376. /* Buttons are mapped as axes in the order they appear in the button enumeration */
  377. {
  378. static int button_axis_offsets[] = {
  379. 24, /* SDL_CONTROLLER_BUTTON_A */
  380. 23, /* SDL_CONTROLLER_BUTTON_B */
  381. 25, /* SDL_CONTROLLER_BUTTON_X */
  382. 22, /* SDL_CONTROLLER_BUTTON_Y */
  383. 0, /* SDL_CONTROLLER_BUTTON_BACK */
  384. 0, /* SDL_CONTROLLER_BUTTON_GUIDE */
  385. 0, /* SDL_CONTROLLER_BUTTON_START */
  386. 0, /* SDL_CONTROLLER_BUTTON_LEFTSTICK */
  387. 0, /* SDL_CONTROLLER_BUTTON_RIGHTSTICK */
  388. 20, /* SDL_CONTROLLER_BUTTON_LEFTSHOULDER */
  389. 21, /* SDL_CONTROLLER_BUTTON_RIGHTSHOULDER */
  390. 14, /* SDL_CONTROLLER_BUTTON_DPAD_UP */
  391. 16, /* SDL_CONTROLLER_BUTTON_DPAD_DOWN */
  392. 17, /* SDL_CONTROLLER_BUTTON_DPAD_LEFT */
  393. 15, /* SDL_CONTROLLER_BUTTON_DPAD_RIGHT */
  394. };
  395. int i, axis_index = 6;
  396. for (i = 0; i < SDL_arraysize(button_axis_offsets); ++i) {
  397. int offset = button_axis_offsets[i];
  398. if (!offset) {
  399. /* This button doesn't report as an axis */
  400. continue;
  401. }
  402. axis = ((int)data[offset] * 257) - 32768;
  403. SDL_PrivateJoystickAxis(joystick, axis_index, axis);
  404. ++axis_index;
  405. }
  406. }
  407. if (ctx->report_sensors) {
  408. float sensor_data[3];
  409. sensor_data[0] = HIDAPI_DriverPS3_ScaleAccel(LOAD16(data[41], data[42]));
  410. sensor_data[1] = -HIDAPI_DriverPS3_ScaleAccel(LOAD16(data[45], data[46]));
  411. sensor_data[2] = -HIDAPI_DriverPS3_ScaleAccel(LOAD16(data[43], data[44]));
  412. SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_ACCEL, 0, sensor_data, SDL_arraysize(sensor_data));
  413. }
  414. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  415. }
  416. static SDL_bool
  417. HIDAPI_DriverPS3_UpdateDevice(SDL_HIDAPI_Device *device)
  418. {
  419. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  420. SDL_Joystick *joystick = NULL;
  421. Uint8 data[USB_PACKET_LENGTH];
  422. int size;
  423. if (device->num_joysticks > 0) {
  424. joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
  425. } else {
  426. return SDL_FALSE;
  427. }
  428. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  429. #ifdef DEBUG_PS3_PROTOCOL
  430. HIDAPI_DumpPacket("PS3 packet: size = %d", data, size);
  431. #endif
  432. if (!joystick) {
  433. continue;
  434. }
  435. if (size == 7) {
  436. /* Seen on a ShanWan PS2 -> PS3 USB converter */
  437. HIDAPI_DriverPS3_HandleMiniStatePacket(joystick, ctx, data, size);
  438. /* Wait for the first report to set the LED state after the controller stops blinking */
  439. if (!ctx->effects_updated) {
  440. HIDAPI_DriverPS3_UpdateEffects(device);
  441. ctx->effects_updated = SDL_TRUE;
  442. }
  443. continue;
  444. }
  445. switch (data[0]) {
  446. case k_EPS3ReportIdState:
  447. if (data[1] == 0xFF) {
  448. /* Invalid data packet, ignore */
  449. break;
  450. }
  451. HIDAPI_DriverPS3_HandleStatePacket(joystick, ctx, data, size);
  452. /* Wait for the first report to set the LED state after the controller stops blinking */
  453. if (!ctx->effects_updated) {
  454. HIDAPI_DriverPS3_UpdateEffects(device);
  455. ctx->effects_updated = SDL_TRUE;
  456. }
  457. break;
  458. default:
  459. #ifdef DEBUG_JOYSTICK
  460. SDL_Log("Unknown PS3 packet: 0x%.2x\n", data[0]);
  461. #endif
  462. break;
  463. }
  464. }
  465. if (size < 0) {
  466. /* Read error, device is disconnected */
  467. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  468. }
  469. return (size >= 0);
  470. }
  471. static void
  472. HIDAPI_DriverPS3_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  473. {
  474. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  475. ctx->joystick = NULL;
  476. }
  477. static void
  478. HIDAPI_DriverPS3_FreeDevice(SDL_HIDAPI_Device *device)
  479. {
  480. }
  481. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS3 =
  482. {
  483. SDL_HINT_JOYSTICK_HIDAPI_PS3,
  484. SDL_TRUE,
  485. HIDAPI_DriverPS3_RegisterHints,
  486. HIDAPI_DriverPS3_UnregisterHints,
  487. HIDAPI_DriverPS3_IsEnabled,
  488. HIDAPI_DriverPS3_IsSupportedDevice,
  489. HIDAPI_DriverPS3_InitDevice,
  490. HIDAPI_DriverPS3_GetDevicePlayerIndex,
  491. HIDAPI_DriverPS3_SetDevicePlayerIndex,
  492. HIDAPI_DriverPS3_UpdateDevice,
  493. HIDAPI_DriverPS3_OpenJoystick,
  494. HIDAPI_DriverPS3_RumbleJoystick,
  495. HIDAPI_DriverPS3_RumbleJoystickTriggers,
  496. HIDAPI_DriverPS3_GetJoystickCapabilities,
  497. HIDAPI_DriverPS3_SetJoystickLED,
  498. HIDAPI_DriverPS3_SendJoystickEffect,
  499. HIDAPI_DriverPS3_SetJoystickSensorsEnabled,
  500. HIDAPI_DriverPS3_CloseJoystick,
  501. HIDAPI_DriverPS3_FreeDevice,
  502. };
  503. static SDL_bool
  504. HIDAPI_DriverPS3ThirdParty_IsEnabled(void)
  505. {
  506. #if 1 /* Not enabled by default, we don't know what the L3/R3 buttons are */
  507. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS3, SDL_FALSE);
  508. #else
  509. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS3,
  510. SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI,
  511. SDL_HIDAPI_DEFAULT));
  512. #endif
  513. }
  514. static SDL_bool
  515. HIDAPI_DriverPS3ThirdParty_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
  516. {
  517. Uint8 data[USB_PACKET_LENGTH];
  518. int size;
  519. if (SONY_THIRDPARTY_VENDOR(vendor_id)) {
  520. if (device) {
  521. if ((size = ReadFeatureReport(device->dev, 0x03, data, sizeof(data))) == 8 &&
  522. data[2] == 0x26) {
  523. /* Supported third party controller */
  524. return SDL_TRUE;
  525. } else {
  526. return SDL_FALSE;
  527. }
  528. } else {
  529. /* Might be supported by this driver, enumerate and find out */
  530. return SDL_TRUE;
  531. }
  532. }
  533. return SDL_FALSE;
  534. }
  535. static SDL_bool
  536. HIDAPI_DriverPS3ThirdParty_InitDevice(SDL_HIDAPI_Device *device)
  537. {
  538. SDL_DriverPS3_Context *ctx;
  539. ctx = (SDL_DriverPS3_Context *)SDL_calloc(1, sizeof(*ctx));
  540. if (!ctx) {
  541. SDL_OutOfMemory();
  542. return SDL_FALSE;
  543. }
  544. ctx->device = device;
  545. device->context = ctx;
  546. device->type = SDL_CONTROLLER_TYPE_PS3;
  547. return HIDAPI_JoystickConnected(device, NULL);
  548. }
  549. static int
  550. HIDAPI_DriverPS3ThirdParty_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  551. {
  552. return -1;
  553. }
  554. static void
  555. HIDAPI_DriverPS3ThirdParty_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  556. {
  557. }
  558. static SDL_bool
  559. HIDAPI_DriverPS3ThirdParty_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  560. {
  561. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  562. ctx->joystick = joystick;
  563. SDL_zeroa(ctx->last_state);
  564. /* Initialize the joystick capabilities */
  565. joystick->nbuttons = 15;
  566. joystick->naxes = 16;
  567. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  568. return SDL_TRUE;
  569. }
  570. static int
  571. HIDAPI_DriverPS3ThirdParty_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  572. {
  573. return SDL_Unsupported();
  574. }
  575. static int
  576. HIDAPI_DriverPS3ThirdParty_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  577. {
  578. return SDL_Unsupported();
  579. }
  580. static Uint32
  581. HIDAPI_DriverPS3ThirdParty_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  582. {
  583. return 0;
  584. }
  585. static int
  586. HIDAPI_DriverPS3ThirdParty_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  587. {
  588. return SDL_Unsupported();
  589. }
  590. static int
  591. HIDAPI_DriverPS3ThirdParty_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size)
  592. {
  593. return SDL_Unsupported();
  594. }
  595. static int
  596. HIDAPI_DriverPS3ThirdParty_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
  597. {
  598. return SDL_Unsupported();
  599. }
  600. static void
  601. HIDAPI_DriverPS3ThirdParty_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverPS3_Context *ctx, Uint8 *data, int size)
  602. {
  603. Sint16 axis;
  604. if (ctx->last_state[0] != data[0]) {
  605. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[0] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  606. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[0] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  607. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[0] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  608. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[0] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  609. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[0] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  610. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[0] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  611. }
  612. if (ctx->last_state[1] != data[1]) {
  613. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[1] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  614. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[1] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  615. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[1] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  616. }
  617. if (ctx->last_state[2] != data[2]) {
  618. SDL_bool dpad_up = SDL_FALSE;
  619. SDL_bool dpad_down = SDL_FALSE;
  620. SDL_bool dpad_left = SDL_FALSE;
  621. SDL_bool dpad_right = SDL_FALSE;
  622. switch (data[2] & 0x0f) {
  623. case 0:
  624. dpad_up = SDL_TRUE;
  625. break;
  626. case 1:
  627. dpad_up = SDL_TRUE;
  628. dpad_right = SDL_TRUE;
  629. break;
  630. case 2:
  631. dpad_right = SDL_TRUE;
  632. break;
  633. case 3:
  634. dpad_right = SDL_TRUE;
  635. dpad_down = SDL_TRUE;
  636. break;
  637. case 4:
  638. dpad_down = SDL_TRUE;
  639. break;
  640. case 5:
  641. dpad_left = SDL_TRUE;
  642. dpad_down = SDL_TRUE;
  643. break;
  644. case 6:
  645. dpad_left = SDL_TRUE;
  646. break;
  647. case 7:
  648. dpad_up = SDL_TRUE;
  649. dpad_left = SDL_TRUE;
  650. break;
  651. default:
  652. break;
  653. }
  654. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  655. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  656. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  657. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  658. }
  659. axis = ((int)data[17] * 257) - 32768;
  660. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  661. axis = ((int)data[18] * 257) - 32768;
  662. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  663. axis = ((int)data[3] * 257) - 32768;
  664. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  665. axis = ((int)data[4] * 257) - 32768;
  666. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  667. axis = ((int)data[5] * 257) - 32768;
  668. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  669. axis = ((int)data[6] * 257) - 32768;
  670. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  671. /* Buttons are mapped as axes in the order they appear in the button enumeration */
  672. {
  673. static int button_axis_offsets[] = {
  674. 13, /* SDL_CONTROLLER_BUTTON_A */
  675. 12, /* SDL_CONTROLLER_BUTTON_B */
  676. 14, /* SDL_CONTROLLER_BUTTON_X */
  677. 11, /* SDL_CONTROLLER_BUTTON_Y */
  678. 0, /* SDL_CONTROLLER_BUTTON_BACK */
  679. 0, /* SDL_CONTROLLER_BUTTON_GUIDE */
  680. 0, /* SDL_CONTROLLER_BUTTON_START */
  681. 0, /* SDL_CONTROLLER_BUTTON_LEFTSTICK */
  682. 0, /* SDL_CONTROLLER_BUTTON_RIGHTSTICK */
  683. 15, /* SDL_CONTROLLER_BUTTON_LEFTSHOULDER */
  684. 16, /* SDL_CONTROLLER_BUTTON_RIGHTSHOULDER */
  685. 9, /* SDL_CONTROLLER_BUTTON_DPAD_UP */
  686. 10, /* SDL_CONTROLLER_BUTTON_DPAD_DOWN */
  687. 8, /* SDL_CONTROLLER_BUTTON_DPAD_LEFT */
  688. 7, /* SDL_CONTROLLER_BUTTON_DPAD_RIGHT */
  689. };
  690. int i, axis_index = 6;
  691. for (i = 0; i < SDL_arraysize(button_axis_offsets); ++i) {
  692. int offset = button_axis_offsets[i];
  693. if (!offset) {
  694. /* This button doesn't report as an axis */
  695. continue;
  696. }
  697. axis = ((int)data[offset] * 257) - 32768;
  698. SDL_PrivateJoystickAxis(joystick, axis_index, axis);
  699. ++axis_index;
  700. }
  701. }
  702. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  703. }
  704. static SDL_bool
  705. HIDAPI_DriverPS3ThirdParty_UpdateDevice(SDL_HIDAPI_Device *device)
  706. {
  707. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  708. SDL_Joystick *joystick = NULL;
  709. Uint8 data[USB_PACKET_LENGTH];
  710. int size;
  711. if (device->num_joysticks > 0) {
  712. joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
  713. } else {
  714. return SDL_FALSE;
  715. }
  716. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  717. #ifdef DEBUG_PS3_PROTOCOL
  718. HIDAPI_DumpPacket("PS3 packet: size = %d", data, size);
  719. #endif
  720. if (!joystick) {
  721. continue;
  722. }
  723. if (size == 27) {
  724. HIDAPI_DriverPS3ThirdParty_HandleStatePacket(joystick, ctx, data, size);
  725. } else {
  726. #ifdef DEBUG_JOYSTICK
  727. SDL_Log("Unknown PS3 packet, size %d\n", size);
  728. #endif
  729. }
  730. }
  731. if (size < 0) {
  732. /* Read error, device is disconnected */
  733. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  734. }
  735. return (size >= 0);
  736. }
  737. static void
  738. HIDAPI_DriverPS3ThirdParty_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  739. {
  740. SDL_DriverPS3_Context *ctx = (SDL_DriverPS3_Context *)device->context;
  741. ctx->joystick = NULL;
  742. }
  743. static void
  744. HIDAPI_DriverPS3ThirdParty_FreeDevice(SDL_HIDAPI_Device *device)
  745. {
  746. }
  747. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS3ThirdParty =
  748. {
  749. SDL_HINT_JOYSTICK_HIDAPI_PS3,
  750. SDL_TRUE,
  751. HIDAPI_DriverPS3_RegisterHints,
  752. HIDAPI_DriverPS3_UnregisterHints,
  753. HIDAPI_DriverPS3ThirdParty_IsEnabled,
  754. HIDAPI_DriverPS3ThirdParty_IsSupportedDevice,
  755. HIDAPI_DriverPS3ThirdParty_InitDevice,
  756. HIDAPI_DriverPS3ThirdParty_GetDevicePlayerIndex,
  757. HIDAPI_DriverPS3ThirdParty_SetDevicePlayerIndex,
  758. HIDAPI_DriverPS3ThirdParty_UpdateDevice,
  759. HIDAPI_DriverPS3ThirdParty_OpenJoystick,
  760. HIDAPI_DriverPS3ThirdParty_RumbleJoystick,
  761. HIDAPI_DriverPS3ThirdParty_RumbleJoystickTriggers,
  762. HIDAPI_DriverPS3ThirdParty_GetJoystickCapabilities,
  763. HIDAPI_DriverPS3ThirdParty_SetJoystickLED,
  764. HIDAPI_DriverPS3ThirdParty_SendJoystickEffect,
  765. HIDAPI_DriverPS3ThirdParty_SetJoystickSensorsEnabled,
  766. HIDAPI_DriverPS3ThirdParty_CloseJoystick,
  767. HIDAPI_DriverPS3ThirdParty_FreeDevice,
  768. };
  769. #endif /* SDL_JOYSTICK_HIDAPI_PS3 */
  770. #endif /* SDL_JOYSTICK_HIDAPI */
  771. /* vi: set ts=4 sw=4 expandtab: */