SDL_sysjoystick.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 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_ANDROID
  20. #include <stdio.h> /* For the definition of NULL */
  21. #include "SDL_error.h"
  22. #include "SDL_events.h"
  23. #include "SDL_joystick.h"
  24. #include "SDL_hints.h"
  25. #include "SDL_assert.h"
  26. #include "SDL_timer.h"
  27. #include "SDL_log.h"
  28. #include "SDL_sysjoystick_c.h"
  29. #include "../SDL_joystick_c.h"
  30. #include "../../core/android/SDL_android.h"
  31. #include "../steam/SDL_steamcontroller.h"
  32. #include "android/keycodes.h"
  33. /* As of platform android-14, android/keycodes.h is missing these defines */
  34. #ifndef AKEYCODE_BUTTON_1
  35. #define AKEYCODE_BUTTON_1 188
  36. #define AKEYCODE_BUTTON_2 189
  37. #define AKEYCODE_BUTTON_3 190
  38. #define AKEYCODE_BUTTON_4 191
  39. #define AKEYCODE_BUTTON_5 192
  40. #define AKEYCODE_BUTTON_6 193
  41. #define AKEYCODE_BUTTON_7 194
  42. #define AKEYCODE_BUTTON_8 195
  43. #define AKEYCODE_BUTTON_9 196
  44. #define AKEYCODE_BUTTON_10 197
  45. #define AKEYCODE_BUTTON_11 198
  46. #define AKEYCODE_BUTTON_12 199
  47. #define AKEYCODE_BUTTON_13 200
  48. #define AKEYCODE_BUTTON_14 201
  49. #define AKEYCODE_BUTTON_15 202
  50. #define AKEYCODE_BUTTON_16 203
  51. #endif
  52. #define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
  53. #define ANDROID_ACCELEROMETER_DEVICE_ID INT_MIN
  54. #define ANDROID_MAX_NBUTTONS 36
  55. static SDL_joylist_item * JoystickByDeviceId(int device_id);
  56. static SDL_joylist_item *SDL_joylist = NULL;
  57. static SDL_joylist_item *SDL_joylist_tail = NULL;
  58. static int numjoysticks = 0;
  59. static int instance_counter = 0;
  60. /* Function to convert Android keyCodes into SDL ones.
  61. * This code manipulation is done to get a sequential list of codes.
  62. * FIXME: This is only suited for the case where we use a fixed number of buttons determined by ANDROID_MAX_NBUTTONS
  63. */
  64. static int
  65. keycode_to_SDL(int keycode)
  66. {
  67. /* FIXME: If this function gets too unwieldy in the future, replace with a lookup table */
  68. int button = 0;
  69. switch(keycode)
  70. {
  71. /* Some gamepad buttons (API 9) */
  72. case AKEYCODE_BUTTON_A:
  73. button = SDL_CONTROLLER_BUTTON_A;
  74. break;
  75. case AKEYCODE_BUTTON_B:
  76. button = SDL_CONTROLLER_BUTTON_B;
  77. break;
  78. case AKEYCODE_BUTTON_X:
  79. button = SDL_CONTROLLER_BUTTON_X;
  80. break;
  81. case AKEYCODE_BUTTON_Y:
  82. button = SDL_CONTROLLER_BUTTON_Y;
  83. break;
  84. case AKEYCODE_BUTTON_L1:
  85. button = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
  86. break;
  87. case AKEYCODE_BUTTON_R1:
  88. button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
  89. break;
  90. case AKEYCODE_BUTTON_THUMBL:
  91. button = SDL_CONTROLLER_BUTTON_LEFTSTICK;
  92. break;
  93. case AKEYCODE_BUTTON_THUMBR:
  94. button = SDL_CONTROLLER_BUTTON_RIGHTSTICK;
  95. break;
  96. case AKEYCODE_BUTTON_START:
  97. button = SDL_CONTROLLER_BUTTON_START;
  98. break;
  99. case AKEYCODE_BACK:
  100. case AKEYCODE_BUTTON_SELECT:
  101. button = SDL_CONTROLLER_BUTTON_BACK;
  102. break;
  103. case AKEYCODE_BUTTON_MODE:
  104. button = SDL_CONTROLLER_BUTTON_GUIDE;
  105. break;
  106. case AKEYCODE_BUTTON_L2:
  107. button = SDL_CONTROLLER_BUTTON_MAX; /* Not supported by GameController */
  108. break;
  109. case AKEYCODE_BUTTON_R2:
  110. button = SDL_CONTROLLER_BUTTON_MAX+1; /* Not supported by GameController */
  111. break;
  112. case AKEYCODE_BUTTON_C:
  113. button = SDL_CONTROLLER_BUTTON_MAX+2; /* Not supported by GameController */
  114. break;
  115. case AKEYCODE_BUTTON_Z:
  116. button = SDL_CONTROLLER_BUTTON_MAX+3; /* Not supported by GameController */
  117. break;
  118. /* D-Pad key codes (API 1) */
  119. case AKEYCODE_DPAD_UP:
  120. button = SDL_CONTROLLER_BUTTON_DPAD_UP;
  121. break;
  122. case AKEYCODE_DPAD_DOWN:
  123. button = SDL_CONTROLLER_BUTTON_DPAD_DOWN;
  124. break;
  125. case AKEYCODE_DPAD_LEFT:
  126. button = SDL_CONTROLLER_BUTTON_DPAD_LEFT;
  127. break;
  128. case AKEYCODE_DPAD_RIGHT:
  129. button = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
  130. break;
  131. case AKEYCODE_DPAD_CENTER:
  132. /* This is handled better by applications as the A button */
  133. /*button = SDL_CONTROLLER_BUTTON_MAX+4; /* Not supported by GameController */
  134. button = SDL_CONTROLLER_BUTTON_A;
  135. break;
  136. /* More gamepad buttons (API 12), these get mapped to 20...35*/
  137. case AKEYCODE_BUTTON_1:
  138. case AKEYCODE_BUTTON_2:
  139. case AKEYCODE_BUTTON_3:
  140. case AKEYCODE_BUTTON_4:
  141. case AKEYCODE_BUTTON_5:
  142. case AKEYCODE_BUTTON_6:
  143. case AKEYCODE_BUTTON_7:
  144. case AKEYCODE_BUTTON_8:
  145. case AKEYCODE_BUTTON_9:
  146. case AKEYCODE_BUTTON_10:
  147. case AKEYCODE_BUTTON_11:
  148. case AKEYCODE_BUTTON_12:
  149. case AKEYCODE_BUTTON_13:
  150. case AKEYCODE_BUTTON_14:
  151. case AKEYCODE_BUTTON_15:
  152. case AKEYCODE_BUTTON_16:
  153. button = keycode - AKEYCODE_BUTTON_1 + SDL_CONTROLLER_BUTTON_MAX + 5;
  154. break;
  155. default:
  156. return -1;
  157. /* break; -Wunreachable-code-break */
  158. }
  159. /* This is here in case future generations, probably with six fingers per hand,
  160. * happily add new cases up above and forget to update the max number of buttons.
  161. */
  162. SDL_assert(button < ANDROID_MAX_NBUTTONS);
  163. return button;
  164. }
  165. int
  166. Android_OnPadDown(int device_id, int keycode)
  167. {
  168. SDL_joylist_item *item;
  169. int button = keycode_to_SDL(keycode);
  170. if (button >= 0) {
  171. item = JoystickByDeviceId(device_id);
  172. if (item && item->joystick) {
  173. SDL_PrivateJoystickButton(item->joystick, button , SDL_PRESSED);
  174. }
  175. return 0;
  176. }
  177. return -1;
  178. }
  179. int
  180. Android_OnPadUp(int device_id, int keycode)
  181. {
  182. SDL_joylist_item *item;
  183. int button = keycode_to_SDL(keycode);
  184. if (button >= 0) {
  185. item = JoystickByDeviceId(device_id);
  186. if (item && item->joystick) {
  187. SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
  188. }
  189. return 0;
  190. }
  191. return -1;
  192. }
  193. int
  194. Android_OnJoy(int device_id, int axis, float value)
  195. {
  196. /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
  197. SDL_joylist_item *item = JoystickByDeviceId(device_id);
  198. if (item && item->joystick) {
  199. SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value));
  200. }
  201. return 0;
  202. }
  203. int
  204. Android_OnHat(int device_id, int hat_id, int x, int y)
  205. {
  206. const Uint8 position_map[3][3] = {
  207. {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
  208. {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
  209. {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
  210. };
  211. if (x >= -1 && x <=1 && y >= -1 && y <= 1) {
  212. SDL_joylist_item *item = JoystickByDeviceId(device_id);
  213. if (item && item->joystick) {
  214. SDL_PrivateJoystickHat(item->joystick, hat_id, position_map[y+1][x+1]);
  215. }
  216. return 0;
  217. }
  218. return -1;
  219. }
  220. int
  221. Android_AddJoystick(int device_id, const char *name, const char *desc, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
  222. {
  223. SDL_JoystickGUID guid;
  224. SDL_joylist_item *item;
  225. if(JoystickByDeviceId(device_id) != NULL || name == NULL) {
  226. return -1;
  227. }
  228. /* the GUID is just the first 16 chars of the name for now */
  229. SDL_zero(guid);
  230. SDL_memcpy(&guid, desc, SDL_min(sizeof(guid), SDL_strlen(desc)));
  231. item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
  232. if (item == NULL) {
  233. return -1;
  234. }
  235. SDL_zerop(item);
  236. item->guid = guid;
  237. item->device_id = device_id;
  238. item->name = SDL_strdup(name);
  239. if (item->name == NULL) {
  240. SDL_free(item);
  241. return -1;
  242. }
  243. item->is_accelerometer = is_accelerometer;
  244. if (nbuttons > -1) {
  245. item->nbuttons = nbuttons;
  246. }
  247. else {
  248. item->nbuttons = ANDROID_MAX_NBUTTONS;
  249. }
  250. item->naxes = naxes;
  251. item->nhats = nhats;
  252. item->nballs = nballs;
  253. item->device_instance = instance_counter++;
  254. if (SDL_joylist_tail == NULL) {
  255. SDL_joylist = SDL_joylist_tail = item;
  256. } else {
  257. SDL_joylist_tail->next = item;
  258. SDL_joylist_tail = item;
  259. }
  260. /* Need to increment the joystick count before we post the event */
  261. ++numjoysticks;
  262. SDL_PrivateJoystickAdded(numjoysticks - 1);
  263. #ifdef DEBUG_JOYSTICK
  264. SDL_Log("Added joystick %s with device_id %d", name, device_id);
  265. #endif
  266. return numjoysticks;
  267. }
  268. int
  269. Android_RemoveJoystick(int device_id)
  270. {
  271. SDL_joylist_item *item = SDL_joylist;
  272. SDL_joylist_item *prev = NULL;
  273. /* Don't call JoystickByDeviceId here or there'll be an infinite loop! */
  274. while (item != NULL) {
  275. if (item->device_id == device_id) {
  276. break;
  277. }
  278. prev = item;
  279. item = item->next;
  280. }
  281. if (item == NULL) {
  282. return -1;
  283. }
  284. if (item->joystick) {
  285. item->joystick->hwdata = NULL;
  286. }
  287. if (prev != NULL) {
  288. prev->next = item->next;
  289. } else {
  290. SDL_assert(SDL_joylist == item);
  291. SDL_joylist = item->next;
  292. }
  293. if (item == SDL_joylist_tail) {
  294. SDL_joylist_tail = prev;
  295. }
  296. /* Need to decrement the joystick count before we post the event */
  297. --numjoysticks;
  298. SDL_PrivateJoystickRemoved(item->device_instance);
  299. #ifdef DEBUG_JOYSTICK
  300. SDL_Log("Removed joystick with device_id %d", device_id);
  301. #endif
  302. SDL_free(item->name);
  303. SDL_free(item);
  304. return numjoysticks;
  305. }
  306. static SDL_bool SteamControllerConnectedCallback(const char *name, SDL_JoystickGUID guid, int *device_instance)
  307. {
  308. SDL_joylist_item *item;
  309. item = (SDL_joylist_item *)SDL_calloc(1, sizeof (SDL_joylist_item));
  310. if (item == NULL) {
  311. return SDL_FALSE;
  312. }
  313. *device_instance = item->device_instance = instance_counter++;
  314. item->device_id = -1;
  315. item->name = SDL_strdup(name);
  316. item->guid = guid;
  317. SDL_GetSteamControllerInputs(&item->nbuttons,
  318. &item->naxes,
  319. &item->nhats);
  320. item->m_bSteamController = SDL_TRUE;
  321. if (SDL_joylist_tail == NULL) {
  322. SDL_joylist = SDL_joylist_tail = item;
  323. } else {
  324. SDL_joylist_tail->next = item;
  325. SDL_joylist_tail = item;
  326. }
  327. /* Need to increment the joystick count before we post the event */
  328. ++numjoysticks;
  329. SDL_PrivateJoystickAdded(numjoysticks - 1);
  330. return SDL_TRUE;
  331. }
  332. static void SteamControllerDisconnectedCallback(int device_instance)
  333. {
  334. SDL_joylist_item *item = SDL_joylist;
  335. SDL_joylist_item *prev = NULL;
  336. while (item != NULL) {
  337. if (item->device_instance == device_instance) {
  338. break;
  339. }
  340. prev = item;
  341. item = item->next;
  342. }
  343. if (item == NULL) {
  344. return;
  345. }
  346. if (item->joystick) {
  347. item->joystick->hwdata = NULL;
  348. }
  349. if (prev != NULL) {
  350. prev->next = item->next;
  351. } else {
  352. SDL_assert(SDL_joylist == item);
  353. SDL_joylist = item->next;
  354. }
  355. if (item == SDL_joylist_tail) {
  356. SDL_joylist_tail = prev;
  357. }
  358. /* Need to decrement the joystick count before we post the event */
  359. --numjoysticks;
  360. SDL_PrivateJoystickRemoved(item->device_instance);
  361. SDL_free(item->name);
  362. SDL_free(item);
  363. }
  364. int
  365. SDL_SYS_JoystickInit(void)
  366. {
  367. SDL_SYS_JoystickDetect();
  368. if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
  369. /* Default behavior, accelerometer as joystick */
  370. Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, ANDROID_ACCELEROMETER_NAME, SDL_TRUE, 0, 3, 0, 0);
  371. }
  372. SDL_InitSteamControllers(SteamControllerConnectedCallback,
  373. SteamControllerDisconnectedCallback);
  374. return (numjoysticks);
  375. }
  376. int
  377. SDL_SYS_NumJoysticks(void)
  378. {
  379. return numjoysticks;
  380. }
  381. void
  382. SDL_SYS_JoystickDetect(void)
  383. {
  384. /* Support for device connect/disconnect is API >= 16 only,
  385. * so we poll every three seconds
  386. * Ref: http://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
  387. */
  388. static Uint32 timeout = 0;
  389. if (!timeout || SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
  390. timeout = SDL_GetTicks() + 3000;
  391. Android_JNI_PollInputDevices();
  392. }
  393. SDL_UpdateSteamControllers();
  394. }
  395. static SDL_joylist_item *
  396. JoystickByDevIndex(int device_index)
  397. {
  398. SDL_joylist_item *item = SDL_joylist;
  399. if ((device_index < 0) || (device_index >= numjoysticks)) {
  400. return NULL;
  401. }
  402. while (device_index > 0) {
  403. SDL_assert(item != NULL);
  404. device_index--;
  405. item = item->next;
  406. }
  407. return item;
  408. }
  409. static SDL_joylist_item *
  410. JoystickByDeviceId(int device_id)
  411. {
  412. SDL_joylist_item *item = SDL_joylist;
  413. while (item != NULL) {
  414. if (item->device_id == device_id) {
  415. return item;
  416. }
  417. item = item->next;
  418. }
  419. /* Joystick not found, try adding it */
  420. SDL_SYS_JoystickDetect();
  421. while (item != NULL) {
  422. if (item->device_id == device_id) {
  423. return item;
  424. }
  425. item = item->next;
  426. }
  427. return NULL;
  428. }
  429. /* Function to get the device-dependent name of a joystick */
  430. const char *
  431. SDL_SYS_JoystickNameForDeviceIndex(int device_index)
  432. {
  433. return JoystickByDevIndex(device_index)->name;
  434. }
  435. /* Function to perform the mapping from device index to the instance id for this index */
  436. SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
  437. {
  438. return JoystickByDevIndex(device_index)->device_instance;
  439. }
  440. /* Function to open a joystick for use.
  441. The joystick to open is specified by the device index.
  442. This should fill the nbuttons and naxes fields of the joystick structure.
  443. It returns 0, or -1 if there is an error.
  444. */
  445. int
  446. SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
  447. {
  448. SDL_joylist_item *item = JoystickByDevIndex(device_index);
  449. if (item == NULL) {
  450. return SDL_SetError("No such device");
  451. }
  452. if (item->joystick != NULL) {
  453. return SDL_SetError("Joystick already opened");
  454. }
  455. joystick->instance_id = item->device_instance;
  456. joystick->hwdata = (struct joystick_hwdata *) item;
  457. item->joystick = joystick;
  458. joystick->nhats = item->nhats;
  459. joystick->nballs = item->nballs;
  460. joystick->nbuttons = item->nbuttons;
  461. joystick->naxes = item->naxes;
  462. return (0);
  463. }
  464. /* Function to determine if this joystick is attached to the system right now */
  465. SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
  466. {
  467. return joystick->hwdata != NULL;
  468. }
  469. void
  470. SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
  471. {
  472. SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
  473. if (item == NULL) {
  474. return;
  475. }
  476. if (item->m_bSteamController) {
  477. SDL_UpdateSteamController(joystick);
  478. return;
  479. }
  480. if (item->is_accelerometer) {
  481. int i;
  482. Sint16 value;
  483. float values[3];
  484. if (Android_JNI_GetAccelerometerValues(values)) {
  485. for (i = 0; i < 3; i++) {
  486. if (values[i] > 1.0f) {
  487. values[i] = 1.0f;
  488. } else if (values[i] < -1.0f) {
  489. values[i] = -1.0f;
  490. }
  491. value = (Sint16)(values[i] * 32767.0f);
  492. SDL_PrivateJoystickAxis(item->joystick, i, value);
  493. }
  494. }
  495. }
  496. }
  497. /* Function to close a joystick after use */
  498. void
  499. SDL_SYS_JoystickClose(SDL_Joystick * joystick)
  500. {
  501. SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
  502. if (item) {
  503. item->joystick = NULL;
  504. }
  505. }
  506. /* Function to perform any system-specific joystick related cleanup */
  507. void
  508. SDL_SYS_JoystickQuit(void)
  509. {
  510. /* We don't have any way to scan for joysticks at init, so don't wipe the list
  511. * of joysticks here in case this is a reinit.
  512. */
  513. #if 0
  514. SDL_joylist_item *item = NULL;
  515. SDL_joylist_item *next = NULL;
  516. for (item = SDL_joylist; item; item = next) {
  517. next = item->next;
  518. SDL_free(item->name);
  519. SDL_free(item);
  520. }
  521. SDL_joylist = SDL_joylist_tail = NULL;
  522. numjoysticks = 0;
  523. instance_counter = 0;
  524. #endif /* 0 */
  525. SDL_QuitSteamControllers();
  526. }
  527. SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index)
  528. {
  529. return JoystickByDevIndex(device_index)->guid;
  530. }
  531. SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
  532. {
  533. SDL_JoystickGUID guid;
  534. if (joystick->hwdata != NULL) {
  535. return ((SDL_joylist_item*)joystick->hwdata)->guid;
  536. }
  537. SDL_zero(guid);
  538. return guid;
  539. }
  540. SDL_bool SDL_SYS_IsDPAD_DeviceIndex(int device_index)
  541. {
  542. return JoystickByDevIndex(device_index)->naxes == 0;
  543. }
  544. #endif /* SDL_JOYSTICK_ANDROID */
  545. /* vi: set ts=4 sw=4 expandtab: */