SDL_sysjoystick.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <[email protected]>
  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_MENU:
  94. case AKEYCODE_BUTTON_START:
  95. button = SDL_CONTROLLER_BUTTON_START;
  96. break;
  97. case AKEYCODE_BACK:
  98. case AKEYCODE_BUTTON_SELECT:
  99. button = SDL_CONTROLLER_BUTTON_BACK;
  100. break;
  101. case AKEYCODE_BUTTON_MODE:
  102. button = SDL_CONTROLLER_BUTTON_GUIDE;
  103. break;
  104. case AKEYCODE_BUTTON_L2:
  105. button = 15;
  106. break;
  107. case AKEYCODE_BUTTON_R2:
  108. button = 16;
  109. break;
  110. case AKEYCODE_BUTTON_C:
  111. button = 17;
  112. break;
  113. case AKEYCODE_BUTTON_Z:
  114. button = 18;
  115. break;
  116. /* D-Pad key codes (API 1) */
  117. case AKEYCODE_DPAD_UP:
  118. button = SDL_CONTROLLER_BUTTON_DPAD_UP;
  119. break;
  120. case AKEYCODE_DPAD_DOWN:
  121. button = SDL_CONTROLLER_BUTTON_DPAD_DOWN;
  122. break;
  123. case AKEYCODE_DPAD_LEFT:
  124. button = SDL_CONTROLLER_BUTTON_DPAD_LEFT;
  125. break;
  126. case AKEYCODE_DPAD_RIGHT:
  127. button = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
  128. break;
  129. case AKEYCODE_DPAD_CENTER:
  130. /* This is handled better by applications as the A button */
  131. /*button = 19;*/
  132. button = SDL_CONTROLLER_BUTTON_A;
  133. break;
  134. /* More gamepad buttons (API 12), these get mapped to 20...35*/
  135. case AKEYCODE_BUTTON_1:
  136. case AKEYCODE_BUTTON_2:
  137. case AKEYCODE_BUTTON_3:
  138. case AKEYCODE_BUTTON_4:
  139. case AKEYCODE_BUTTON_5:
  140. case AKEYCODE_BUTTON_6:
  141. case AKEYCODE_BUTTON_7:
  142. case AKEYCODE_BUTTON_8:
  143. case AKEYCODE_BUTTON_9:
  144. case AKEYCODE_BUTTON_10:
  145. case AKEYCODE_BUTTON_11:
  146. case AKEYCODE_BUTTON_12:
  147. case AKEYCODE_BUTTON_13:
  148. case AKEYCODE_BUTTON_14:
  149. case AKEYCODE_BUTTON_15:
  150. case AKEYCODE_BUTTON_16:
  151. button = 20 + (keycode - AKEYCODE_BUTTON_1);
  152. break;
  153. default:
  154. return -1;
  155. /* break; -Wunreachable-code-break */
  156. }
  157. /* This is here in case future generations, probably with six fingers per hand,
  158. * happily add new cases up above and forget to update the max number of buttons.
  159. */
  160. SDL_assert(button < ANDROID_MAX_NBUTTONS);
  161. return button;
  162. }
  163. static SDL_Scancode
  164. button_to_scancode(int button)
  165. {
  166. switch (button) {
  167. case SDL_CONTROLLER_BUTTON_A:
  168. return SDL_SCANCODE_RETURN;
  169. case SDL_CONTROLLER_BUTTON_B:
  170. return SDL_SCANCODE_ESCAPE;
  171. case SDL_CONTROLLER_BUTTON_BACK:
  172. return SDL_SCANCODE_ESCAPE;
  173. case SDL_CONTROLLER_BUTTON_DPAD_UP:
  174. return SDL_SCANCODE_UP;
  175. case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
  176. return SDL_SCANCODE_DOWN;
  177. case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
  178. return SDL_SCANCODE_LEFT;
  179. case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
  180. return SDL_SCANCODE_RIGHT;
  181. }
  182. /* Unsupported button */
  183. return SDL_SCANCODE_UNKNOWN;
  184. }
  185. int
  186. Android_OnPadDown(int device_id, int keycode)
  187. {
  188. SDL_joylist_item *item;
  189. int button = keycode_to_SDL(keycode);
  190. if (button >= 0) {
  191. item = JoystickByDeviceId(device_id);
  192. if (item && item->joystick) {
  193. SDL_PrivateJoystickButton(item->joystick, button, SDL_PRESSED);
  194. } else {
  195. SDL_SendKeyboardKey(SDL_PRESSED, button_to_scancode(button));
  196. }
  197. return 0;
  198. }
  199. return -1;
  200. }
  201. int
  202. Android_OnPadUp(int device_id, int keycode)
  203. {
  204. SDL_joylist_item *item;
  205. int button = keycode_to_SDL(keycode);
  206. if (button >= 0) {
  207. item = JoystickByDeviceId(device_id);
  208. if (item && item->joystick) {
  209. SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
  210. } else {
  211. SDL_SendKeyboardKey(SDL_RELEASED, button_to_scancode(button));
  212. }
  213. return 0;
  214. }
  215. return -1;
  216. }
  217. int
  218. Android_OnJoy(int device_id, int axis, float value)
  219. {
  220. /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
  221. SDL_joylist_item *item = JoystickByDeviceId(device_id);
  222. if (item && item->joystick) {
  223. SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value));
  224. }
  225. return 0;
  226. }
  227. int
  228. Android_OnHat(int device_id, int hat_id, int x, int y)
  229. {
  230. const int DPAD_UP_MASK = (1 << SDL_CONTROLLER_BUTTON_DPAD_UP);
  231. const int DPAD_DOWN_MASK = (1 << SDL_CONTROLLER_BUTTON_DPAD_DOWN);
  232. const int DPAD_LEFT_MASK = (1 << SDL_CONTROLLER_BUTTON_DPAD_LEFT);
  233. const int DPAD_RIGHT_MASK = (1 << SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
  234. if (x >= -1 && x <= 1 && y >= -1 && y <= 1) {
  235. SDL_joylist_item *item = JoystickByDeviceId(device_id);
  236. if (item && item->joystick) {
  237. int dpad_state = 0;
  238. int dpad_delta;
  239. if (x < 0) {
  240. dpad_state |= DPAD_LEFT_MASK;
  241. } else if (x > 0) {
  242. dpad_state |= DPAD_RIGHT_MASK;
  243. }
  244. if (y < 0) {
  245. dpad_state |= DPAD_UP_MASK;
  246. } else if (y > 0) {
  247. dpad_state |= DPAD_DOWN_MASK;
  248. }
  249. dpad_delta = (dpad_state ^ item->dpad_state);
  250. if (dpad_delta) {
  251. if (dpad_delta & DPAD_UP_MASK) {
  252. SDL_PrivateJoystickButton(item->joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (dpad_state & DPAD_UP_MASK) ? SDL_PRESSED : SDL_RELEASED);
  253. }
  254. if (dpad_delta & DPAD_DOWN_MASK) {
  255. SDL_PrivateJoystickButton(item->joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (dpad_state & DPAD_DOWN_MASK) ? SDL_PRESSED : SDL_RELEASED);
  256. }
  257. if (dpad_delta & DPAD_LEFT_MASK) {
  258. SDL_PrivateJoystickButton(item->joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (dpad_state & DPAD_LEFT_MASK) ? SDL_PRESSED : SDL_RELEASED);
  259. }
  260. if (dpad_delta & DPAD_RIGHT_MASK) {
  261. SDL_PrivateJoystickButton(item->joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (dpad_state & DPAD_RIGHT_MASK) ? SDL_PRESSED : SDL_RELEASED);
  262. }
  263. item->dpad_state = dpad_state;
  264. }
  265. }
  266. return 0;
  267. }
  268. return -1;
  269. }
  270. int
  271. 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)
  272. {
  273. SDL_joylist_item *item;
  274. SDL_JoystickGUID guid;
  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. guid = SDL_CreateJoystickGUID(SDL_HARDWARE_BUS_BLUETOOTH, vendor_id, product_id, 0, desc, 0, 0);
  319. /* Update the GUID with capability bits */
  320. {
  321. Uint16 *guid16 = (Uint16 *)guid.data;
  322. guid16[6] = SDL_SwapLE16(button_mask);
  323. guid16[7] = SDL_SwapLE16(axis_mask);
  324. }
  325. item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
  326. if (item == NULL) {
  327. return -1;
  328. }
  329. SDL_zerop(item);
  330. item->guid = guid;
  331. item->device_id = device_id;
  332. item->name = SDL_CreateJoystickName(vendor_id, product_id, NULL, name);
  333. if (item->name == NULL) {
  334. SDL_free(item);
  335. return -1;
  336. }
  337. item->is_accelerometer = is_accelerometer;
  338. if (button_mask == 0xFFFFFFFF) {
  339. item->nbuttons = ANDROID_MAX_NBUTTONS;
  340. } else {
  341. for (i = 0; i < sizeof(button_mask)*8; ++i) {
  342. if (button_mask & (1 << i)) {
  343. item->nbuttons = i+1;
  344. }
  345. }
  346. }
  347. item->naxes = naxes;
  348. item->nhats = nhats;
  349. item->nballs = nballs;
  350. item->device_instance = SDL_GetNextJoystickInstanceID();
  351. if (SDL_joylist_tail == NULL) {
  352. SDL_joylist = SDL_joylist_tail = item;
  353. } else {
  354. SDL_joylist_tail->next = item;
  355. SDL_joylist_tail = item;
  356. }
  357. /* Need to increment the joystick count before we post the event */
  358. ++numjoysticks;
  359. SDL_PrivateJoystickAdded(item->device_instance);
  360. #ifdef DEBUG_JOYSTICK
  361. SDL_Log("Added joystick %s with device_id %d", item->name, device_id);
  362. #endif
  363. return numjoysticks;
  364. }
  365. int
  366. Android_RemoveJoystick(int device_id)
  367. {
  368. SDL_joylist_item *item = SDL_joylist;
  369. SDL_joylist_item *prev = NULL;
  370. /* Don't call JoystickByDeviceId here or there'll be an infinite loop! */
  371. while (item != NULL) {
  372. if (item->device_id == device_id) {
  373. break;
  374. }
  375. prev = item;
  376. item = item->next;
  377. }
  378. if (item == NULL) {
  379. return -1;
  380. }
  381. if (item->joystick) {
  382. item->joystick->hwdata = NULL;
  383. }
  384. if (prev != NULL) {
  385. prev->next = item->next;
  386. } else {
  387. SDL_assert(SDL_joylist == item);
  388. SDL_joylist = item->next;
  389. }
  390. if (item == SDL_joylist_tail) {
  391. SDL_joylist_tail = prev;
  392. }
  393. /* Need to decrement the joystick count before we post the event */
  394. --numjoysticks;
  395. SDL_PrivateJoystickRemoved(item->device_instance);
  396. #ifdef DEBUG_JOYSTICK
  397. SDL_Log("Removed joystick with device_id %d", device_id);
  398. #endif
  399. SDL_free(item->name);
  400. SDL_free(item);
  401. return numjoysticks;
  402. }
  403. static void ANDROID_JoystickDetect(void);
  404. static int
  405. ANDROID_JoystickInit(void)
  406. {
  407. ANDROID_JoystickDetect();
  408. if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
  409. /* Default behavior, accelerometer as joystick */
  410. Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, ANDROID_ACCELEROMETER_NAME, 0, 0, SDL_TRUE, 0, 3, 0, 0);
  411. }
  412. return 0;
  413. }
  414. static int
  415. ANDROID_JoystickGetCount(void)
  416. {
  417. return numjoysticks;
  418. }
  419. static void
  420. ANDROID_JoystickDetect(void)
  421. {
  422. /* Support for device connect/disconnect is API >= 16 only,
  423. * so we poll every three seconds
  424. * Ref: http://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
  425. */
  426. static Uint32 timeout = 0;
  427. if (!timeout || SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
  428. timeout = SDL_GetTicks() + 3000;
  429. Android_JNI_PollInputDevices();
  430. }
  431. }
  432. static SDL_joylist_item *
  433. JoystickByDevIndex(int device_index)
  434. {
  435. SDL_joylist_item *item = SDL_joylist;
  436. if ((device_index < 0) || (device_index >= numjoysticks)) {
  437. return NULL;
  438. }
  439. while (device_index > 0) {
  440. SDL_assert(item != NULL);
  441. device_index--;
  442. item = item->next;
  443. }
  444. return item;
  445. }
  446. static SDL_joylist_item *
  447. JoystickByDeviceId(int device_id)
  448. {
  449. SDL_joylist_item *item = SDL_joylist;
  450. while (item != NULL) {
  451. if (item->device_id == device_id) {
  452. return item;
  453. }
  454. item = item->next;
  455. }
  456. /* Joystick not found, try adding it */
  457. ANDROID_JoystickDetect();
  458. while (item != NULL) {
  459. if (item->device_id == device_id) {
  460. return item;
  461. }
  462. item = item->next;
  463. }
  464. return NULL;
  465. }
  466. static const char *
  467. ANDROID_JoystickGetDeviceName(int device_index)
  468. {
  469. return JoystickByDevIndex(device_index)->name;
  470. }
  471. static const char *
  472. ANDROID_JoystickGetDevicePath(int device_index)
  473. {
  474. return NULL;
  475. }
  476. static int
  477. ANDROID_JoystickGetDevicePlayerIndex(int device_index)
  478. {
  479. return -1;
  480. }
  481. static void
  482. ANDROID_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  483. {
  484. }
  485. static SDL_JoystickGUID
  486. ANDROID_JoystickGetDeviceGUID(int device_index)
  487. {
  488. return JoystickByDevIndex(device_index)->guid;
  489. }
  490. static SDL_JoystickID
  491. ANDROID_JoystickGetDeviceInstanceID(int device_index)
  492. {
  493. return JoystickByDevIndex(device_index)->device_instance;
  494. }
  495. static int
  496. ANDROID_JoystickOpen(SDL_Joystick *joystick, int device_index)
  497. {
  498. SDL_joylist_item *item = JoystickByDevIndex(device_index);
  499. if (item == NULL) {
  500. return SDL_SetError("No such device");
  501. }
  502. if (item->joystick != NULL) {
  503. return SDL_SetError("Joystick already opened");
  504. }
  505. joystick->instance_id = item->device_instance;
  506. joystick->hwdata = (struct joystick_hwdata *) item;
  507. item->joystick = joystick;
  508. joystick->nhats = item->nhats;
  509. joystick->nballs = item->nballs;
  510. joystick->nbuttons = item->nbuttons;
  511. joystick->naxes = item->naxes;
  512. return (0);
  513. }
  514. static int
  515. ANDROID_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  516. {
  517. return SDL_Unsupported();
  518. }
  519. static int
  520. ANDROID_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  521. {
  522. return SDL_Unsupported();
  523. }
  524. static Uint32
  525. ANDROID_JoystickGetCapabilities(SDL_Joystick *joystick)
  526. {
  527. return 0;
  528. }
  529. static int
  530. ANDROID_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  531. {
  532. return SDL_Unsupported();
  533. }
  534. static int
  535. ANDROID_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
  536. {
  537. return SDL_Unsupported();
  538. }
  539. static int
  540. ANDROID_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
  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. static SDL_bool
  595. ANDROID_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  596. {
  597. return SDL_FALSE;
  598. }
  599. SDL_JoystickDriver SDL_ANDROID_JoystickDriver =
  600. {
  601. ANDROID_JoystickInit,
  602. ANDROID_JoystickGetCount,
  603. ANDROID_JoystickDetect,
  604. ANDROID_JoystickGetDeviceName,
  605. ANDROID_JoystickGetDevicePath,
  606. ANDROID_JoystickGetDevicePlayerIndex,
  607. ANDROID_JoystickSetDevicePlayerIndex,
  608. ANDROID_JoystickGetDeviceGUID,
  609. ANDROID_JoystickGetDeviceInstanceID,
  610. ANDROID_JoystickOpen,
  611. ANDROID_JoystickRumble,
  612. ANDROID_JoystickRumbleTriggers,
  613. ANDROID_JoystickGetCapabilities,
  614. ANDROID_JoystickSetLED,
  615. ANDROID_JoystickSendEffect,
  616. ANDROID_JoystickSetSensorsEnabled,
  617. ANDROID_JoystickUpdate,
  618. ANDROID_JoystickClose,
  619. ANDROID_JoystickQuit,
  620. ANDROID_JoystickGetGamepadMapping
  621. };
  622. #endif /* SDL_JOYSTICK_ANDROID */
  623. /* vi: set ts=4 sw=4 expandtab: */