SDL_sysjoystick.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 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_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 "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. static int instance_counter = 0;
  59. /* Function to convert Android keyCodes into SDL ones.
  60. * This code manipulation is done to get a sequential list of codes.
  61. * FIXME: This is only suited for the case where we use a fixed number of buttons determined by ANDROID_MAX_NBUTTONS
  62. */
  63. static int
  64. keycode_to_SDL(int keycode)
  65. {
  66. /* FIXME: If this function gets too unwiedly in the future, replace with a lookup table */
  67. int button = 0;
  68. switch(keycode)
  69. {
  70. /* Some gamepad buttons (API 9) */
  71. case AKEYCODE_BUTTON_A:
  72. button = SDL_CONTROLLER_BUTTON_A;
  73. break;
  74. case AKEYCODE_BUTTON_B:
  75. button = SDL_CONTROLLER_BUTTON_B;
  76. break;
  77. case AKEYCODE_BUTTON_X:
  78. button = SDL_CONTROLLER_BUTTON_X;
  79. break;
  80. case AKEYCODE_BUTTON_Y:
  81. button = SDL_CONTROLLER_BUTTON_Y;
  82. break;
  83. case AKEYCODE_BUTTON_L1:
  84. button = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
  85. break;
  86. case AKEYCODE_BUTTON_R1:
  87. button = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
  88. break;
  89. case AKEYCODE_BUTTON_THUMBL:
  90. button = SDL_CONTROLLER_BUTTON_LEFTSTICK;
  91. break;
  92. case AKEYCODE_BUTTON_THUMBR:
  93. button = SDL_CONTROLLER_BUTTON_RIGHTSTICK;
  94. break;
  95. case AKEYCODE_BUTTON_START:
  96. button = SDL_CONTROLLER_BUTTON_START;
  97. break;
  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 = SDL_CONTROLLER_BUTTON_MAX; /* Not supported by GameController */
  106. break;
  107. case AKEYCODE_BUTTON_R2:
  108. button = SDL_CONTROLLER_BUTTON_MAX+1; /* Not supported by GameController */
  109. break;
  110. case AKEYCODE_BUTTON_C:
  111. button = SDL_CONTROLLER_BUTTON_MAX+2; /* Not supported by GameController */
  112. break;
  113. case AKEYCODE_BUTTON_Z:
  114. button = SDL_CONTROLLER_BUTTON_MAX+3; /* Not supported by GameController */
  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. button = SDL_CONTROLLER_BUTTON_MAX+4; /* Not supported by GameController */
  131. break;
  132. /* More gamepad buttons (API 12), these get mapped to 20...35*/
  133. case AKEYCODE_BUTTON_1:
  134. case AKEYCODE_BUTTON_2:
  135. case AKEYCODE_BUTTON_3:
  136. case AKEYCODE_BUTTON_4:
  137. case AKEYCODE_BUTTON_5:
  138. case AKEYCODE_BUTTON_6:
  139. case AKEYCODE_BUTTON_7:
  140. case AKEYCODE_BUTTON_8:
  141. case AKEYCODE_BUTTON_9:
  142. case AKEYCODE_BUTTON_10:
  143. case AKEYCODE_BUTTON_11:
  144. case AKEYCODE_BUTTON_12:
  145. case AKEYCODE_BUTTON_13:
  146. case AKEYCODE_BUTTON_14:
  147. case AKEYCODE_BUTTON_15:
  148. case AKEYCODE_BUTTON_16:
  149. button = keycode - AKEYCODE_BUTTON_1 + SDL_CONTROLLER_BUTTON_MAX + 5;
  150. break;
  151. default:
  152. return -1;
  153. break;
  154. }
  155. /* This is here in case future generations, probably with six fingers per hand,
  156. * happily add new cases up above and forget to update the max number of buttons.
  157. */
  158. SDL_assert(button < ANDROID_MAX_NBUTTONS);
  159. return button;
  160. }
  161. int
  162. Android_OnPadDown(int device_id, int keycode)
  163. {
  164. SDL_joylist_item *item;
  165. int button = keycode_to_SDL(keycode);
  166. if (button >= 0) {
  167. item = JoystickByDeviceId(device_id);
  168. if (item && item->joystick) {
  169. SDL_PrivateJoystickButton(item->joystick, button , SDL_PRESSED);
  170. }
  171. return 0;
  172. }
  173. return -1;
  174. }
  175. int
  176. Android_OnPadUp(int device_id, int keycode)
  177. {
  178. SDL_joylist_item *item;
  179. int button = keycode_to_SDL(keycode);
  180. if (button >= 0) {
  181. item = JoystickByDeviceId(device_id);
  182. if (item && item->joystick) {
  183. SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
  184. }
  185. return 0;
  186. }
  187. return -1;
  188. }
  189. int
  190. Android_OnJoy(int device_id, int axis, float value)
  191. {
  192. /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
  193. SDL_joylist_item *item = JoystickByDeviceId(device_id);
  194. if (item && item->joystick) {
  195. SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value) );
  196. }
  197. return 0;
  198. }
  199. int
  200. Android_OnHat(int device_id, int hat_id, int x, int y)
  201. {
  202. const Uint8 position_map[3][3] = {
  203. {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
  204. {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
  205. {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
  206. };
  207. if (x >= -1 && x <=1 && y >= -1 && y <= 1) {
  208. SDL_joylist_item *item = JoystickByDeviceId(device_id);
  209. if (item && item->joystick) {
  210. SDL_PrivateJoystickHat(item->joystick, hat_id, position_map[y+1][x+1] );
  211. }
  212. return 0;
  213. }
  214. return -1;
  215. }
  216. int
  217. Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
  218. {
  219. SDL_JoystickGUID guid;
  220. SDL_joylist_item *item;
  221. if(JoystickByDeviceId(device_id) != NULL || name == NULL) {
  222. return -1;
  223. }
  224. /* the GUID is just the first 16 chars of the name for now */
  225. SDL_zero( guid );
  226. SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
  227. item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
  228. if (item == NULL) {
  229. return -1;
  230. }
  231. SDL_zerop(item);
  232. item->guid = guid;
  233. item->device_id = device_id;
  234. item->name = SDL_strdup(name);
  235. if ( item->name == NULL ) {
  236. SDL_free(item);
  237. return -1;
  238. }
  239. item->is_accelerometer = is_accelerometer;
  240. if (nbuttons > -1) {
  241. item->nbuttons = nbuttons;
  242. }
  243. else {
  244. item->nbuttons = ANDROID_MAX_NBUTTONS;
  245. }
  246. item->naxes = naxes;
  247. item->nhats = nhats;
  248. item->nballs = nballs;
  249. item->device_instance = instance_counter++;
  250. if (SDL_joylist_tail == NULL) {
  251. SDL_joylist = SDL_joylist_tail = item;
  252. } else {
  253. SDL_joylist_tail->next = item;
  254. SDL_joylist_tail = item;
  255. }
  256. /* Need to increment the joystick count before we post the event */
  257. ++numjoysticks;
  258. SDL_PrivateJoystickAdded(numjoysticks - 1);
  259. #ifdef DEBUG_JOYSTICK
  260. SDL_Log("Added joystick %s with device_id %d", name, device_id);
  261. #endif
  262. return numjoysticks;
  263. }
  264. int
  265. Android_RemoveJoystick(int device_id)
  266. {
  267. SDL_joylist_item *item = SDL_joylist;
  268. SDL_joylist_item *prev = NULL;
  269. /* Don't call JoystickByDeviceId here or there'll be an infinite loop! */
  270. while (item != NULL) {
  271. if (item->device_id == device_id) {
  272. break;
  273. }
  274. prev = item;
  275. item = item->next;
  276. }
  277. if (item == NULL) {
  278. return -1;
  279. }
  280. if (item->joystick) {
  281. item->joystick->hwdata = NULL;
  282. }
  283. if (prev != NULL) {
  284. prev->next = item->next;
  285. } else {
  286. SDL_assert(SDL_joylist == item);
  287. SDL_joylist = item->next;
  288. }
  289. if (item == SDL_joylist_tail) {
  290. SDL_joylist_tail = prev;
  291. }
  292. /* Need to decrement the joystick count before we post the event */
  293. --numjoysticks;
  294. SDL_PrivateJoystickRemoved(item->device_instance);
  295. #ifdef DEBUG_JOYSTICK
  296. SDL_Log("Removed joystick with device_id %d", device_id);
  297. #endif
  298. SDL_free(item->name);
  299. SDL_free(item);
  300. return numjoysticks;
  301. }
  302. int
  303. SDL_SYS_JoystickInit(void)
  304. {
  305. SDL_SYS_JoystickDetect();
  306. if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
  307. /* Default behavior, accelerometer as joystick */
  308. Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, SDL_TRUE, 0, 3, 0, 0);
  309. }
  310. return (numjoysticks);
  311. }
  312. int SDL_SYS_NumJoysticks()
  313. {
  314. return numjoysticks;
  315. }
  316. void SDL_SYS_JoystickDetect()
  317. {
  318. /* Support for device connect/disconnect is API >= 16 only,
  319. * so we poll every three seconds
  320. * Ref: http://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
  321. */
  322. static Uint32 timeout = 0;
  323. if (SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
  324. timeout = SDL_GetTicks() + 3000;
  325. Android_JNI_PollInputDevices();
  326. }
  327. }
  328. static SDL_joylist_item *
  329. JoystickByDevIndex(int device_index)
  330. {
  331. SDL_joylist_item *item = SDL_joylist;
  332. if ((device_index < 0) || (device_index >= numjoysticks)) {
  333. return NULL;
  334. }
  335. while (device_index > 0) {
  336. SDL_assert(item != NULL);
  337. device_index--;
  338. item = item->next;
  339. }
  340. return item;
  341. }
  342. static SDL_joylist_item *
  343. JoystickByDeviceId(int device_id)
  344. {
  345. SDL_joylist_item *item = SDL_joylist;
  346. while (item != NULL) {
  347. if (item->device_id == device_id) {
  348. return item;
  349. }
  350. item = item->next;
  351. }
  352. /* Joystick not found, try adding it */
  353. SDL_SYS_JoystickDetect();
  354. while (item != NULL) {
  355. if (item->device_id == device_id) {
  356. return item;
  357. }
  358. item = item->next;
  359. }
  360. return NULL;
  361. }
  362. /* Function to get the device-dependent name of a joystick */
  363. const char *
  364. SDL_SYS_JoystickNameForDeviceIndex(int device_index)
  365. {
  366. return JoystickByDevIndex(device_index)->name;
  367. }
  368. /* Function to perform the mapping from device index to the instance id for this index */
  369. SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
  370. {
  371. return JoystickByDevIndex(device_index)->device_instance;
  372. }
  373. /* Function to open a joystick for use.
  374. The joystick to open is specified by the device index.
  375. This should fill the nbuttons and naxes fields of the joystick structure.
  376. It returns 0, or -1 if there is an error.
  377. */
  378. int
  379. SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
  380. {
  381. SDL_joylist_item *item = JoystickByDevIndex(device_index);
  382. if (item == NULL ) {
  383. return SDL_SetError("No such device");
  384. }
  385. if (item->joystick != NULL) {
  386. return SDL_SetError("Joystick already opened");
  387. }
  388. joystick->instance_id = item->device_instance;
  389. joystick->hwdata = (struct joystick_hwdata *) item;
  390. item->joystick = joystick;
  391. joystick->nhats = item->nhats;
  392. joystick->nballs = item->nballs;
  393. joystick->nbuttons = item->nbuttons;
  394. joystick->naxes = item->naxes;
  395. return (0);
  396. }
  397. /* Function to determine if this joystick is attached to the system right now */
  398. SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
  399. {
  400. return joystick->hwdata != NULL;
  401. }
  402. void
  403. SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
  404. {
  405. int i;
  406. Sint16 value;
  407. float values[3];
  408. SDL_joylist_item *item = SDL_joylist;
  409. while (item) {
  410. if (item->is_accelerometer) {
  411. if (item->joystick) {
  412. if (Android_JNI_GetAccelerometerValues(values)) {
  413. for ( i = 0; i < 3; i++ ) {
  414. if (values[i] > 1.0f) {
  415. values[i] = 1.0f;
  416. } else if (values[i] < -1.0f) {
  417. values[i] = -1.0f;
  418. }
  419. value = (Sint16)(values[i] * 32767.0f);
  420. SDL_PrivateJoystickAxis(item->joystick, i, value);
  421. }
  422. }
  423. }
  424. break;
  425. }
  426. item = item->next;
  427. }
  428. }
  429. /* Function to close a joystick after use */
  430. void
  431. SDL_SYS_JoystickClose(SDL_Joystick * joystick)
  432. {
  433. SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
  434. if (item) {
  435. item->joystick = NULL;
  436. }
  437. }
  438. /* Function to perform any system-specific joystick related cleanup */
  439. void
  440. SDL_SYS_JoystickQuit(void)
  441. {
  442. SDL_joylist_item *item = NULL;
  443. SDL_joylist_item *next = NULL;
  444. for (item = SDL_joylist; item; item = next) {
  445. next = item->next;
  446. SDL_free(item->name);
  447. SDL_free(item);
  448. }
  449. SDL_joylist = SDL_joylist_tail = NULL;
  450. numjoysticks = 0;
  451. instance_counter = 0;
  452. }
  453. SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
  454. {
  455. return JoystickByDevIndex(device_index)->guid;
  456. }
  457. SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
  458. {
  459. SDL_JoystickGUID guid;
  460. if (joystick->hwdata != NULL) {
  461. return ((SDL_joylist_item*)joystick->hwdata)->guid;
  462. }
  463. SDL_zero(guid);
  464. return guid;
  465. }
  466. #endif /* SDL_JOYSTICK_ANDROID */
  467. /* vi: set ts=4 sw=4 expandtab: */