SDL_sysjoystick.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 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 "../../events/SDL_keyboard_c.h"
  31. #include "../../core/android/SDL_android.h"
  32. #include "../hidapi/SDL_hidapijoystick_c.h"
  33. #include "android/keycodes.h"
  34. /* As of platform android-14, android/keycodes.h is missing these defines */
  35. #ifndef AKEYCODE_BUTTON_1
  36. #define AKEYCODE_BUTTON_1 188
  37. #define AKEYCODE_BUTTON_2 189
  38. #define AKEYCODE_BUTTON_3 190
  39. #define AKEYCODE_BUTTON_4 191
  40. #define AKEYCODE_BUTTON_5 192
  41. #define AKEYCODE_BUTTON_6 193
  42. #define AKEYCODE_BUTTON_7 194
  43. #define AKEYCODE_BUTTON_8 195
  44. #define AKEYCODE_BUTTON_9 196
  45. #define AKEYCODE_BUTTON_10 197
  46. #define AKEYCODE_BUTTON_11 198
  47. #define AKEYCODE_BUTTON_12 199
  48. #define AKEYCODE_BUTTON_13 200
  49. #define AKEYCODE_BUTTON_14 201
  50. #define AKEYCODE_BUTTON_15 202
  51. #define AKEYCODE_BUTTON_16 203
  52. #endif
  53. #define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
  54. #define ANDROID_ACCELEROMETER_DEVICE_ID INT_MIN
  55. #define ANDROID_MAX_NBUTTONS 36
  56. static SDL_joylist_item * JoystickByDeviceId(int device_id);
  57. static SDL_joylist_item *SDL_joylist = NULL;
  58. static SDL_joylist_item *SDL_joylist_tail = NULL;
  59. static int numjoysticks = 0;
  60. /* Public domain CRC implementation adapted from:
  61. http://home.thep.lu.se/~bjorn/crc/crc32_simple.c
  62. */
  63. static Uint32 crc32_for_byte(Uint32 r)
  64. {
  65. int i;
  66. for(i = 0; i < 8; ++i) {
  67. r = (r & 1? 0: (Uint32)0xEDB88320L) ^ r >> 1;
  68. }
  69. return r ^ (Uint32)0xFF000000L;
  70. }
  71. static Uint32 crc32(const void *data, size_t count)
  72. {
  73. Uint32 crc = 0;
  74. int i;
  75. for(i = 0; i < count; ++i) {
  76. crc = crc32_for_byte((Uint8)crc ^ ((const Uint8*)data)[i]) ^ crc >> 8;
  77. }
  78. return crc;
  79. }
  80. /* Function to convert Android keyCodes into SDL ones.
  81. * This code manipulation is done to get a sequential list of codes.
  82. * FIXME: This is only suited for the case where we use a fixed number of buttons determined by ANDROID_MAX_NBUTTONS
  83. */
  84. static int
  85. keycode_to_SDL(int keycode)
  86. {
  87. /* FIXME: If this function gets too unwieldy in the future, replace with a lookup table */
  88. int button = 0;
  89. switch (keycode) {
  90. /* Some gamepad buttons (API 9) */
  91. case AKEYCODE_BUTTON_A:
  92. button = SDL_CONTROLLER_BUTTON_A;
  93. break;
  94. case AKEYCODE_BUTTON_B:
  95. button = SDL_CONTROLLER_BUTTON_B;
  96. break;
  97. case AKEYCODE_BUTTON_X:
  98. button = SDL_CONTROLLER_BUTTON_X;
  99. break;
  100. case AKEYCODE_BUTTON_Y:
  101. button = SDL_CONTROLLER_BUTTON_Y;
  102. break;
  103. case AKEYCODE_BUTTON_L1:
  104. button = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
  105. break;
  106. case AKEYCODE_BUTTON_R1:
  107. button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
  108. break;
  109. case AKEYCODE_BUTTON_THUMBL:
  110. button = SDL_CONTROLLER_BUTTON_LEFTSTICK;
  111. break;
  112. case AKEYCODE_BUTTON_THUMBR:
  113. button = SDL_CONTROLLER_BUTTON_RIGHTSTICK;
  114. break;
  115. case AKEYCODE_BUTTON_START:
  116. button = SDL_CONTROLLER_BUTTON_START;
  117. break;
  118. case AKEYCODE_BACK:
  119. case AKEYCODE_BUTTON_SELECT:
  120. button = SDL_CONTROLLER_BUTTON_BACK;
  121. break;
  122. case AKEYCODE_BUTTON_MODE:
  123. button = SDL_CONTROLLER_BUTTON_GUIDE;
  124. break;
  125. case AKEYCODE_BUTTON_L2:
  126. button = SDL_CONTROLLER_BUTTON_MAX; /* Not supported by GameController */
  127. break;
  128. case AKEYCODE_BUTTON_R2:
  129. button = SDL_CONTROLLER_BUTTON_MAX+1; /* Not supported by GameController */
  130. break;
  131. case AKEYCODE_BUTTON_C:
  132. button = SDL_CONTROLLER_BUTTON_MAX+2; /* Not supported by GameController */
  133. break;
  134. case AKEYCODE_BUTTON_Z:
  135. button = SDL_CONTROLLER_BUTTON_MAX+3; /* Not supported by GameController */
  136. break;
  137. /* D-Pad key codes (API 1) */
  138. case AKEYCODE_DPAD_UP:
  139. button = SDL_CONTROLLER_BUTTON_DPAD_UP;
  140. break;
  141. case AKEYCODE_DPAD_DOWN:
  142. button = SDL_CONTROLLER_BUTTON_DPAD_DOWN;
  143. break;
  144. case AKEYCODE_DPAD_LEFT:
  145. button = SDL_CONTROLLER_BUTTON_DPAD_LEFT;
  146. break;
  147. case AKEYCODE_DPAD_RIGHT:
  148. button = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
  149. break;
  150. case AKEYCODE_DPAD_CENTER:
  151. /* This is handled better by applications as the A button */
  152. /*button = SDL_CONTROLLER_BUTTON_MAX+4;*/ /* Not supported by GameController */
  153. button = SDL_CONTROLLER_BUTTON_A;
  154. break;
  155. /* More gamepad buttons (API 12), these get mapped to 20...35*/
  156. case AKEYCODE_BUTTON_1:
  157. case AKEYCODE_BUTTON_2:
  158. case AKEYCODE_BUTTON_3:
  159. case AKEYCODE_BUTTON_4:
  160. case AKEYCODE_BUTTON_5:
  161. case AKEYCODE_BUTTON_6:
  162. case AKEYCODE_BUTTON_7:
  163. case AKEYCODE_BUTTON_8:
  164. case AKEYCODE_BUTTON_9:
  165. case AKEYCODE_BUTTON_10:
  166. case AKEYCODE_BUTTON_11:
  167. case AKEYCODE_BUTTON_12:
  168. case AKEYCODE_BUTTON_13:
  169. case AKEYCODE_BUTTON_14:
  170. case AKEYCODE_BUTTON_15:
  171. case AKEYCODE_BUTTON_16:
  172. button = keycode - AKEYCODE_BUTTON_1 + SDL_CONTROLLER_BUTTON_MAX + 5;
  173. break;
  174. default:
  175. return -1;
  176. /* break; -Wunreachable-code-break */
  177. }
  178. /* This is here in case future generations, probably with six fingers per hand,
  179. * happily add new cases up above and forget to update the max number of buttons.
  180. */
  181. SDL_assert(button < ANDROID_MAX_NBUTTONS);
  182. return button;
  183. }
  184. static SDL_Scancode
  185. button_to_scancode(int button)
  186. {
  187. switch (button) {
  188. case SDL_CONTROLLER_BUTTON_A:
  189. return SDL_SCANCODE_RETURN;
  190. case SDL_CONTROLLER_BUTTON_B:
  191. return SDL_SCANCODE_ESCAPE;
  192. case SDL_CONTROLLER_BUTTON_BACK:
  193. return SDL_SCANCODE_ESCAPE;
  194. case SDL_CONTROLLER_BUTTON_DPAD_UP:
  195. return SDL_SCANCODE_UP;
  196. case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
  197. return SDL_SCANCODE_DOWN;
  198. case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
  199. return SDL_SCANCODE_LEFT;
  200. case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
  201. return SDL_SCANCODE_RIGHT;
  202. }
  203. /* Unsupported button */
  204. return SDL_SCANCODE_UNKNOWN;
  205. }
  206. int
  207. Android_OnPadDown(int device_id, int keycode)
  208. {
  209. SDL_joylist_item *item;
  210. int button = keycode_to_SDL(keycode);
  211. if (button >= 0) {
  212. item = JoystickByDeviceId(device_id);
  213. if (item && item->joystick) {
  214. SDL_PrivateJoystickButton(item->joystick, button, SDL_PRESSED);
  215. } else {
  216. SDL_SendKeyboardKey(SDL_PRESSED, button_to_scancode(button));
  217. }
  218. return 0;
  219. }
  220. return -1;
  221. }
  222. int
  223. Android_OnPadUp(int device_id, int keycode)
  224. {
  225. SDL_joylist_item *item;
  226. int button = keycode_to_SDL(keycode);
  227. if (button >= 0) {
  228. item = JoystickByDeviceId(device_id);
  229. if (item && item->joystick) {
  230. SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
  231. } else {
  232. SDL_SendKeyboardKey(SDL_RELEASED, button_to_scancode(button));
  233. }
  234. return 0;
  235. }
  236. return -1;
  237. }
  238. int
  239. Android_OnJoy(int device_id, int axis, float value)
  240. {
  241. /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
  242. SDL_joylist_item *item = JoystickByDeviceId(device_id);
  243. if (item && item->joystick) {
  244. SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value));
  245. }
  246. return 0;
  247. }
  248. int
  249. Android_OnHat(int device_id, int hat_id, int x, int y)
  250. {
  251. const int DPAD_UP_MASK = (1 << SDL_CONTROLLER_BUTTON_DPAD_UP);
  252. const int DPAD_DOWN_MASK = (1 << SDL_CONTROLLER_BUTTON_DPAD_DOWN);
  253. const int DPAD_LEFT_MASK = (1 << SDL_CONTROLLER_BUTTON_DPAD_LEFT);
  254. const int DPAD_RIGHT_MASK = (1 << SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
  255. if (x >= -1 && x <= 1 && y >= -1 && y <= 1) {
  256. SDL_joylist_item *item = JoystickByDeviceId(device_id);
  257. if (item && item->joystick) {
  258. int dpad_state = 0;
  259. int dpad_delta;
  260. if (x < 0) {
  261. dpad_state |= DPAD_LEFT_MASK;
  262. } else if (x > 0) {
  263. dpad_state |= DPAD_RIGHT_MASK;
  264. }
  265. if (y < 0) {
  266. dpad_state |= DPAD_UP_MASK;
  267. } else if (y > 0) {
  268. dpad_state |= DPAD_DOWN_MASK;
  269. }
  270. dpad_delta = (dpad_state ^ item->dpad_state);
  271. if (dpad_delta) {
  272. if (dpad_delta & DPAD_UP_MASK) {
  273. SDL_PrivateJoystickButton(item->joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (dpad_state & DPAD_UP_MASK) ? SDL_PRESSED : SDL_RELEASED);
  274. }
  275. if (dpad_delta & DPAD_DOWN_MASK) {
  276. SDL_PrivateJoystickButton(item->joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (dpad_state & DPAD_DOWN_MASK) ? SDL_PRESSED : SDL_RELEASED);
  277. }
  278. if (dpad_delta & DPAD_LEFT_MASK) {
  279. SDL_PrivateJoystickButton(item->joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (dpad_state & DPAD_LEFT_MASK) ? SDL_PRESSED : SDL_RELEASED);
  280. }
  281. if (dpad_delta & DPAD_RIGHT_MASK) {
  282. SDL_PrivateJoystickButton(item->joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (dpad_state & DPAD_RIGHT_MASK) ? SDL_PRESSED : SDL_RELEASED);
  283. }
  284. item->dpad_state = dpad_state;
  285. }
  286. }
  287. return 0;
  288. }
  289. return -1;
  290. }
  291. int
  292. Android_AddJoystick(int device_id, const char *name, const char *desc, int vendor_id, int product_id, SDL_bool is_accelerometer, int button_mask, int naxes, int nhats, int nballs)
  293. {
  294. SDL_joylist_item *item;
  295. SDL_JoystickGUID guid;
  296. Uint16 *guid16 = (Uint16 *)guid.data;
  297. int i;
  298. int axis_mask;
  299. if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
  300. /* Ignore devices that aren't actually controllers (e.g. remotes), they'll be handled as keyboard input */
  301. if (naxes < 2 && nhats < 1) {
  302. return -1;
  303. }
  304. }
  305. if (JoystickByDeviceId(device_id) != NULL || name == NULL) {
  306. return -1;
  307. }
  308. #ifdef SDL_JOYSTICK_HIDAPI
  309. if (HIDAPI_IsDevicePresent(vendor_id, product_id, 0, name)) {
  310. /* The HIDAPI driver is taking care of this device */
  311. return -1;
  312. }
  313. #endif
  314. #ifdef DEBUG_JOYSTICK
  315. SDL_Log("Joystick: %s, descriptor %s, vendor = 0x%.4x, product = 0x%.4x, %d axes, %d hats\n", name, desc, vendor_id, product_id, naxes, nhats);
  316. #endif
  317. /* Add the available buttons and axes
  318. The axis mask should probably come from Java where there is more information about the axes...
  319. */
  320. axis_mask = 0;
  321. if (!is_accelerometer) {
  322. if (naxes >= 2) {
  323. axis_mask |= ((1 << SDL_CONTROLLER_AXIS_LEFTX) | (1 << SDL_CONTROLLER_AXIS_LEFTY));
  324. }
  325. if (naxes >= 4) {
  326. axis_mask |= ((1 << SDL_CONTROLLER_AXIS_RIGHTX) | (1 << SDL_CONTROLLER_AXIS_RIGHTY));
  327. }
  328. if (naxes >= 6) {
  329. axis_mask |= ((1 << SDL_CONTROLLER_AXIS_TRIGGERLEFT) | (1 << SDL_CONTROLLER_AXIS_TRIGGERRIGHT));
  330. }
  331. }
  332. if (nhats > 0) {
  333. /* Hat is translated into DPAD buttons */
  334. button_mask |= ((1 << SDL_CONTROLLER_BUTTON_DPAD_UP) |
  335. (1 << SDL_CONTROLLER_BUTTON_DPAD_DOWN) |
  336. (1 << SDL_CONTROLLER_BUTTON_DPAD_LEFT) |
  337. (1 << SDL_CONTROLLER_BUTTON_DPAD_RIGHT));
  338. nhats = 0;
  339. }
  340. SDL_memset(guid.data, 0, sizeof(guid.data));
  341. /* We only need 16 bits for each of these; space them out to fill 128. */
  342. /* Byteswap so devices get same GUID on little/big endian platforms. */
  343. *guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_BLUETOOTH);
  344. *guid16++ = 0;
  345. if (vendor_id && product_id) {
  346. *guid16++ = SDL_SwapLE16(vendor_id);
  347. *guid16++ = 0;
  348. *guid16++ = SDL_SwapLE16(product_id);
  349. *guid16++ = 0;
  350. } else {
  351. Uint32 crc = crc32(desc, SDL_strlen(desc));
  352. SDL_memcpy(guid16, desc, SDL_min(2*sizeof(*guid16), SDL_strlen(desc)));
  353. guid16 += 2;
  354. *(Uint32 *)guid16 = SDL_SwapLE32(crc);
  355. guid16 += 2;
  356. }
  357. *guid16++ = SDL_SwapLE16(button_mask);
  358. *guid16++ = SDL_SwapLE16(axis_mask);
  359. item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
  360. if (item == NULL) {
  361. return -1;
  362. }
  363. SDL_zerop(item);
  364. item->guid = guid;
  365. item->device_id = device_id;
  366. item->name = SDL_strdup(name);
  367. if (item->name == NULL) {
  368. SDL_free(item);
  369. return -1;
  370. }
  371. item->is_accelerometer = is_accelerometer;
  372. if (button_mask == 0xFFFFFFFF) {
  373. item->nbuttons = ANDROID_MAX_NBUTTONS;
  374. } else {
  375. for (i = 0; i < sizeof(button_mask)*8; ++i) {
  376. if (button_mask & (1 << i)) {
  377. item->nbuttons = i+1;
  378. }
  379. }
  380. }
  381. item->naxes = naxes;
  382. item->nhats = nhats;
  383. item->nballs = nballs;
  384. item->device_instance = SDL_GetNextJoystickInstanceID();
  385. if (SDL_joylist_tail == NULL) {
  386. SDL_joylist = SDL_joylist_tail = item;
  387. } else {
  388. SDL_joylist_tail->next = item;
  389. SDL_joylist_tail = item;
  390. }
  391. /* Need to increment the joystick count before we post the event */
  392. ++numjoysticks;
  393. SDL_PrivateJoystickAdded(item->device_instance);
  394. #ifdef DEBUG_JOYSTICK
  395. SDL_Log("Added joystick %s with device_id %d", name, device_id);
  396. #endif
  397. return numjoysticks;
  398. }
  399. int
  400. Android_RemoveJoystick(int device_id)
  401. {
  402. SDL_joylist_item *item = SDL_joylist;
  403. SDL_joylist_item *prev = NULL;
  404. /* Don't call JoystickByDeviceId here or there'll be an infinite loop! */
  405. while (item != NULL) {
  406. if (item->device_id == device_id) {
  407. break;
  408. }
  409. prev = item;
  410. item = item->next;
  411. }
  412. if (item == NULL) {
  413. return -1;
  414. }
  415. if (item->joystick) {
  416. item->joystick->hwdata = NULL;
  417. }
  418. if (prev != NULL) {
  419. prev->next = item->next;
  420. } else {
  421. SDL_assert(SDL_joylist == item);
  422. SDL_joylist = item->next;
  423. }
  424. if (item == SDL_joylist_tail) {
  425. SDL_joylist_tail = prev;
  426. }
  427. /* Need to decrement the joystick count before we post the event */
  428. --numjoysticks;
  429. SDL_PrivateJoystickRemoved(item->device_instance);
  430. #ifdef DEBUG_JOYSTICK
  431. SDL_Log("Removed joystick with device_id %d", device_id);
  432. #endif
  433. SDL_free(item->name);
  434. SDL_free(item);
  435. return numjoysticks;
  436. }
  437. static void ANDROID_JoystickDetect(void);
  438. static int
  439. ANDROID_JoystickInit(void)
  440. {
  441. ANDROID_JoystickDetect();
  442. if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
  443. /* Default behavior, accelerometer as joystick */
  444. Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, ANDROID_ACCELEROMETER_NAME, 0, 0, SDL_TRUE, 0, 3, 0, 0);
  445. }
  446. return 0;
  447. }
  448. static int
  449. ANDROID_JoystickGetCount(void)
  450. {
  451. return numjoysticks;
  452. }
  453. static void
  454. ANDROID_JoystickDetect(void)
  455. {
  456. /* Support for device connect/disconnect is API >= 16 only,
  457. * so we poll every three seconds
  458. * Ref: http://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
  459. */
  460. static Uint32 timeout = 0;
  461. if (!timeout || SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
  462. timeout = SDL_GetTicks() + 3000;
  463. Android_JNI_PollInputDevices();
  464. }
  465. }
  466. static SDL_joylist_item *
  467. JoystickByDevIndex(int device_index)
  468. {
  469. SDL_joylist_item *item = SDL_joylist;
  470. if ((device_index < 0) || (device_index >= numjoysticks)) {
  471. return NULL;
  472. }
  473. while (device_index > 0) {
  474. SDL_assert(item != NULL);
  475. device_index--;
  476. item = item->next;
  477. }
  478. return item;
  479. }
  480. static SDL_joylist_item *
  481. JoystickByDeviceId(int device_id)
  482. {
  483. SDL_joylist_item *item = SDL_joylist;
  484. while (item != NULL) {
  485. if (item->device_id == device_id) {
  486. return item;
  487. }
  488. item = item->next;
  489. }
  490. /* Joystick not found, try adding it */
  491. ANDROID_JoystickDetect();
  492. while (item != NULL) {
  493. if (item->device_id == device_id) {
  494. return item;
  495. }
  496. item = item->next;
  497. }
  498. return NULL;
  499. }
  500. static const char *
  501. ANDROID_JoystickGetDeviceName(int device_index)
  502. {
  503. return JoystickByDevIndex(device_index)->name;
  504. }
  505. static int
  506. ANDROID_JoystickGetDevicePlayerIndex(int device_index)
  507. {
  508. return -1;
  509. }
  510. static SDL_JoystickGUID
  511. ANDROID_JoystickGetDeviceGUID(int device_index)
  512. {
  513. return JoystickByDevIndex(device_index)->guid;
  514. }
  515. static SDL_JoystickID
  516. ANDROID_JoystickGetDeviceInstanceID(int device_index)
  517. {
  518. return JoystickByDevIndex(device_index)->device_instance;
  519. }
  520. static int
  521. ANDROID_JoystickOpen(SDL_Joystick * joystick, int device_index)
  522. {
  523. SDL_joylist_item *item = JoystickByDevIndex(device_index);
  524. if (item == NULL) {
  525. return SDL_SetError("No such device");
  526. }
  527. if (item->joystick != NULL) {
  528. return SDL_SetError("Joystick already opened");
  529. }
  530. joystick->instance_id = item->device_instance;
  531. joystick->hwdata = (struct joystick_hwdata *) item;
  532. item->joystick = joystick;
  533. joystick->nhats = item->nhats;
  534. joystick->nballs = item->nballs;
  535. joystick->nbuttons = item->nbuttons;
  536. joystick->naxes = item->naxes;
  537. return (0);
  538. }
  539. static int
  540. ANDROID_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  541. {
  542. return SDL_Unsupported();
  543. }
  544. static void
  545. ANDROID_JoystickUpdate(SDL_Joystick * joystick)
  546. {
  547. SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
  548. if (item == NULL) {
  549. return;
  550. }
  551. if (item->is_accelerometer) {
  552. int i;
  553. Sint16 value;
  554. float values[3];
  555. if (Android_JNI_GetAccelerometerValues(values)) {
  556. for (i = 0; i < 3; i++) {
  557. if (values[i] > 1.0f) {
  558. values[i] = 1.0f;
  559. } else if (values[i] < -1.0f) {
  560. values[i] = -1.0f;
  561. }
  562. value = (Sint16)(values[i] * 32767.0f);
  563. SDL_PrivateJoystickAxis(item->joystick, i, value);
  564. }
  565. }
  566. }
  567. }
  568. static void
  569. ANDROID_JoystickClose(SDL_Joystick * joystick)
  570. {
  571. SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
  572. if (item) {
  573. item->joystick = NULL;
  574. }
  575. }
  576. static void
  577. ANDROID_JoystickQuit(void)
  578. {
  579. /* We don't have any way to scan for joysticks at init, so don't wipe the list
  580. * of joysticks here in case this is a reinit.
  581. */
  582. #if 0
  583. SDL_joylist_item *item = NULL;
  584. SDL_joylist_item *next = NULL;
  585. for (item = SDL_joylist; item; item = next) {
  586. next = item->next;
  587. SDL_free(item->name);
  588. SDL_free(item);
  589. }
  590. SDL_joylist = SDL_joylist_tail = NULL;
  591. numjoysticks = 0;
  592. #endif /* 0 */
  593. }
  594. SDL_JoystickDriver SDL_ANDROID_JoystickDriver =
  595. {
  596. ANDROID_JoystickInit,
  597. ANDROID_JoystickGetCount,
  598. ANDROID_JoystickDetect,
  599. ANDROID_JoystickGetDeviceName,
  600. ANDROID_JoystickGetDevicePlayerIndex,
  601. ANDROID_JoystickGetDeviceGUID,
  602. ANDROID_JoystickGetDeviceInstanceID,
  603. ANDROID_JoystickOpen,
  604. ANDROID_JoystickRumble,
  605. ANDROID_JoystickUpdate,
  606. ANDROID_JoystickClose,
  607. ANDROID_JoystickQuit,
  608. };
  609. #endif /* SDL_JOYSTICK_ANDROID */
  610. /* vi: set ts=4 sw=4 expandtab: */