SDL_sysjoystick.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2013 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_config.h"
  19. #ifdef SDL_JOYSTICK_ANDROID
  20. /* This is the system specific header for the SDL joystick API */
  21. #include <stdio.h> /* For the definition of NULL */
  22. #include "SDL_error.h"
  23. #include "SDL_events.h"
  24. #include "SDL_joystick.h"
  25. #include "SDL_hints.h"
  26. #include "SDL_assert.h"
  27. #include "../SDL_sysjoystick.h"
  28. #include "../SDL_joystick_c.h"
  29. #include "../../core/android/SDL_android.h"
  30. #include "android/keycodes.h"
  31. /* As of platform android-14, android/keycodes.h is missing these defines */
  32. #ifndef AKEYCODE_BUTTON_1
  33. #define AKEYCODE_BUTTON_1 188
  34. #define AKEYCODE_BUTTON_2 189
  35. #define AKEYCODE_BUTTON_3 190
  36. #define AKEYCODE_BUTTON_4 191
  37. #define AKEYCODE_BUTTON_5 192
  38. #define AKEYCODE_BUTTON_6 193
  39. #define AKEYCODE_BUTTON_7 194
  40. #define AKEYCODE_BUTTON_8 195
  41. #define AKEYCODE_BUTTON_9 196
  42. #define AKEYCODE_BUTTON_10 197
  43. #define AKEYCODE_BUTTON_11 198
  44. #define AKEYCODE_BUTTON_12 199
  45. #define AKEYCODE_BUTTON_13 200
  46. #define AKEYCODE_BUTTON_14 201
  47. #define AKEYCODE_BUTTON_15 202
  48. #define AKEYCODE_BUTTON_16 203
  49. #endif
  50. #define ANDROID_ACCELEROMETER_INDEX (SYS_numjoysticks - 1)
  51. #define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
  52. #define ANDROID_MAX_NBUTTONS 36
  53. static SDL_Joystick **SYS_Joysticks;
  54. static char **SYS_JoystickNames;
  55. static int SYS_numjoysticks;
  56. static SDL_bool SYS_accelAsJoy;
  57. /* Function to convert Android keyCodes into SDL ones.
  58. * This code manipulation is done to get a sequential list of codes.
  59. * FIXME: This is only suited for the case where we use a fixed number of buttons determined by ANDROID_MAX_NBUTTONS
  60. */
  61. static int
  62. keycode_to_SDL(int keycode)
  63. {
  64. /* FIXME: If this function gets too unwiedly in the future, replace with a lookup table */
  65. int button = 0;
  66. switch(keycode)
  67. {
  68. /* D-Pad key codes (API 1), these get mapped to 0...4 */
  69. case AKEYCODE_DPAD_UP:
  70. case AKEYCODE_DPAD_DOWN:
  71. case AKEYCODE_DPAD_LEFT:
  72. case AKEYCODE_DPAD_RIGHT:
  73. case AKEYCODE_DPAD_CENTER:
  74. button = keycode - AKEYCODE_DPAD_UP;
  75. break;
  76. /* Some gamepad buttons (API 9), these get mapped to 5...19*/
  77. case AKEYCODE_BUTTON_A:
  78. case AKEYCODE_BUTTON_B:
  79. case AKEYCODE_BUTTON_C:
  80. case AKEYCODE_BUTTON_X:
  81. case AKEYCODE_BUTTON_Y:
  82. case AKEYCODE_BUTTON_Z:
  83. case AKEYCODE_BUTTON_L1:
  84. case AKEYCODE_BUTTON_L2:
  85. case AKEYCODE_BUTTON_R1:
  86. case AKEYCODE_BUTTON_R2:
  87. case AKEYCODE_BUTTON_THUMBL:
  88. case AKEYCODE_BUTTON_THUMBR:
  89. case AKEYCODE_BUTTON_START:
  90. case AKEYCODE_BUTTON_SELECT:
  91. case AKEYCODE_BUTTON_MODE:
  92. button = keycode - AKEYCODE_BUTTON_A + 5;
  93. break;
  94. /* More gamepad buttons (API 12), these get mapped to 20...35*/
  95. case AKEYCODE_BUTTON_1:
  96. case AKEYCODE_BUTTON_2:
  97. case AKEYCODE_BUTTON_3:
  98. case AKEYCODE_BUTTON_4:
  99. case AKEYCODE_BUTTON_5:
  100. case AKEYCODE_BUTTON_6:
  101. case AKEYCODE_BUTTON_7:
  102. case AKEYCODE_BUTTON_8:
  103. case AKEYCODE_BUTTON_9:
  104. case AKEYCODE_BUTTON_10:
  105. case AKEYCODE_BUTTON_11:
  106. case AKEYCODE_BUTTON_12:
  107. case AKEYCODE_BUTTON_13:
  108. case AKEYCODE_BUTTON_14:
  109. case AKEYCODE_BUTTON_15:
  110. case AKEYCODE_BUTTON_16:
  111. button = keycode - AKEYCODE_BUTTON_1 + 20;
  112. break;
  113. default:
  114. return -1;
  115. break;
  116. }
  117. /* This is here in case future generations, probably with six fingers per hand,
  118. * happily add new cases up above and forget to update the max number of buttons.
  119. */
  120. SDL_assert(button < ANDROID_MAX_NBUTTONS);
  121. return button;
  122. }
  123. /* Function to scan the system for joysticks.
  124. * This function should set SDL_numjoysticks to the number of available
  125. * joysticks. Joystick 0 should be the system default joystick.
  126. * It should return 0, or -1 on an unrecoverable fatal error.
  127. */
  128. int
  129. SDL_SYS_JoystickInit(void)
  130. {
  131. int i = 0;
  132. const char *env;
  133. env = SDL_GetHint(SDL_HINT_ACCEL_AS_JOY);
  134. if (env && !SDL_atoi(env))
  135. SYS_accelAsJoy = SDL_FALSE;
  136. else
  137. SYS_accelAsJoy = SDL_TRUE; /* Default behavior */
  138. SYS_numjoysticks = Android_JNI_GetNumJoysticks();
  139. if (SYS_accelAsJoy) {
  140. SYS_numjoysticks++;
  141. }
  142. SYS_Joysticks = (SDL_Joystick **)SDL_calloc(1, SYS_numjoysticks*sizeof(SDL_Joystick *));
  143. if (SYS_Joysticks == NULL)
  144. {
  145. return SDL_OutOfMemory();
  146. }
  147. SYS_JoystickNames = (char **)SDL_calloc(1, SYS_numjoysticks*sizeof(char *));
  148. if (SYS_JoystickNames == NULL)
  149. {
  150. SDL_free(SYS_Joysticks);
  151. SYS_Joysticks = NULL;
  152. return SDL_OutOfMemory();
  153. }
  154. for (i = 0; i < SYS_numjoysticks; i++)
  155. {
  156. if ( SYS_accelAsJoy && i == ANDROID_ACCELEROMETER_INDEX ) {
  157. SYS_JoystickNames[i] = ANDROID_ACCELEROMETER_NAME;
  158. } else {
  159. SYS_JoystickNames[i] = Android_JNI_GetJoystickName(i);
  160. }
  161. }
  162. return (SYS_numjoysticks);
  163. }
  164. int SDL_SYS_NumJoysticks()
  165. {
  166. return SYS_numjoysticks;
  167. }
  168. void SDL_SYS_JoystickDetect()
  169. {
  170. }
  171. /* TODO: Hotplugging support */
  172. SDL_bool SDL_SYS_JoystickNeedsPolling()
  173. {
  174. return SDL_FALSE;
  175. }
  176. /* Function to get the device-dependent name of a joystick */
  177. const char *
  178. SDL_SYS_JoystickNameForDeviceIndex(int device_index)
  179. {
  180. return SYS_JoystickNames[device_index];
  181. }
  182. /* Function to perform the mapping from device index to the instance id for this index */
  183. SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
  184. {
  185. return device_index;
  186. }
  187. /* Function to open a joystick for use.
  188. The joystick to open is specified by the index field of the joystick.
  189. This should fill the nbuttons and naxes fields of the joystick structure.
  190. It returns 0, or -1 if there is an error.
  191. */
  192. int
  193. SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
  194. {
  195. if (device_index < SYS_numjoysticks) {
  196. joystick->nhats = 0;
  197. joystick->nballs = 0;
  198. if (SYS_accelAsJoy && device_index == ANDROID_ACCELEROMETER_INDEX) {
  199. joystick->nbuttons = 0;
  200. joystick->naxes = 3;
  201. } else {
  202. /* FIXME: Get the real number of buttons in the device? */
  203. joystick->nbuttons = ANDROID_MAX_NBUTTONS;
  204. joystick->naxes = Android_JNI_GetJoystickAxes(device_index);
  205. }
  206. SYS_Joysticks[device_index] = joystick;
  207. return 0;
  208. } else {
  209. return SDL_SetError("No joystick available with that index");
  210. }
  211. }
  212. /* Function to determine is this joystick is attached to the system right now */
  213. SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
  214. {
  215. return SDL_TRUE;
  216. }
  217. /* Function to update the state of a joystick - called as a device poll.
  218. * This function shouldn't update the joystick structure directly,
  219. * but instead should call SDL_PrivateJoystick*() to deliver events
  220. * and update joystick device state.
  221. */
  222. void
  223. SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
  224. {
  225. int i;
  226. Sint16 value;
  227. float values[3];
  228. if (SYS_accelAsJoy && Android_JNI_GetAccelerometerValues(values) &&
  229. joystick->instance_id == ANDROID_ACCELEROMETER_INDEX) {
  230. for ( i = 0; i < 3; i++ ) {
  231. value = (Sint16)(values[i] * 32767.0f);
  232. SDL_PrivateJoystickAxis(joystick, i, value);
  233. }
  234. }
  235. }
  236. /* Function to close a joystick after use */
  237. void
  238. SDL_SYS_JoystickClose(SDL_Joystick * joystick)
  239. {
  240. int device_index;
  241. for (device_index = 0; device_index < SYS_numjoysticks; device_index++) {
  242. if ( SYS_Joysticks[device_index] == joystick ) {
  243. SYS_Joysticks[device_index] = NULL;
  244. }
  245. }
  246. joystick->closed = 1;
  247. }
  248. /* Function to perform any system-specific joystick related cleanup */
  249. void
  250. SDL_SYS_JoystickQuit(void)
  251. {
  252. SDL_free(SYS_JoystickNames);
  253. SDL_free(SYS_Joysticks);
  254. SYS_JoystickNames = NULL;
  255. SYS_Joysticks = NULL;
  256. }
  257. SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
  258. {
  259. SDL_JoystickGUID guid;
  260. /* the GUID is just the first 16 chars of the name for now */
  261. const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
  262. SDL_zero( guid );
  263. SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
  264. return guid;
  265. }
  266. SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
  267. {
  268. SDL_JoystickGUID guid;
  269. /* the GUID is just the first 16 chars of the name for now */
  270. const char *name = joystick->name;
  271. SDL_zero( guid );
  272. SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
  273. return guid;
  274. }
  275. int
  276. Android_OnPadDown(int padId, int keycode)
  277. {
  278. int button = keycode_to_SDL(keycode);
  279. if (button >= 0) {
  280. if (SYS_Joysticks[padId]) {
  281. SDL_PrivateJoystickButton(SYS_Joysticks[padId], button , SDL_PRESSED);
  282. }
  283. return 0;
  284. }
  285. return -1;
  286. }
  287. int
  288. Android_OnPadUp(int padId, int keycode)
  289. {
  290. int button = keycode_to_SDL(keycode);
  291. if (button >= 0) {
  292. if (SYS_Joysticks[padId]) {
  293. SDL_PrivateJoystickButton(SYS_Joysticks[padId], button, SDL_RELEASED);
  294. }
  295. return 0;
  296. }
  297. return -1;
  298. }
  299. int
  300. Android_OnJoy(int joyId, int axis, float value)
  301. {
  302. /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
  303. /* TODO: Are the reported values right? */
  304. if (SYS_Joysticks[joyId]) {
  305. SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axis, (Sint16) (32767.*value) );
  306. }
  307. return 0;
  308. }
  309. #endif /* SDL_JOYSTICK_ANDROID */
  310. /* vi: set ts=4 sw=4 expandtab: */