SDL_sysjoystick.c 20 KB

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