SDL_syshaptic.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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_HAPTIC_ANDROID
  20. #include "SDL_timer.h"
  21. #include "SDL_syshaptic_c.h"
  22. #include "../SDL_syshaptic.h"
  23. #include "SDL_haptic.h"
  24. #include "../../core/android/SDL_android.h"
  25. #include "SDL_joystick.h"
  26. #include "../../joystick/SDL_sysjoystick.h" /* For the real SDL_Joystick */
  27. #include "../../joystick/android/SDL_sysjoystick_c.h" /* For joystick hwdata */
  28. typedef struct SDL_hapticlist_item
  29. {
  30. int device_id;
  31. char *name;
  32. SDL_Haptic *haptic;
  33. struct SDL_hapticlist_item *next;
  34. } SDL_hapticlist_item;
  35. static SDL_hapticlist_item *SDL_hapticlist = NULL;
  36. static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
  37. static int numhaptics = 0;
  38. int
  39. SDL_SYS_HapticInit(void)
  40. {
  41. /* Support for device connect/disconnect is API >= 16 only,
  42. * so we poll every three seconds
  43. * Ref: http://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
  44. */
  45. static Uint32 timeout = 0;
  46. if (SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
  47. timeout = SDL_GetTicks() + 3000;
  48. Android_JNI_PollHapticDevices();
  49. }
  50. return (numhaptics);
  51. }
  52. int
  53. SDL_SYS_NumHaptics(void)
  54. {
  55. return (numhaptics);
  56. }
  57. static SDL_hapticlist_item *
  58. HapticByOrder(int index)
  59. {
  60. SDL_hapticlist_item *item = SDL_hapticlist;
  61. if ((index < 0) || (index >= numhaptics)) {
  62. return NULL;
  63. }
  64. while (index > 0) {
  65. SDL_assert(item != NULL);
  66. --index;
  67. item = item->next;
  68. }
  69. return item;
  70. }
  71. static SDL_hapticlist_item *
  72. HapticByDevId (int device_id)
  73. {
  74. SDL_hapticlist_item *item;
  75. for (item = SDL_hapticlist; item != NULL; item = item->next) {
  76. if (device_id == item->device_id) {
  77. /*SDL_Log("=+=+=+=+=+= HapticByDevId id [%d]", device_id);*/
  78. return item;
  79. }
  80. }
  81. return NULL;
  82. }
  83. const char *
  84. SDL_SYS_HapticName(int index)
  85. {
  86. SDL_hapticlist_item *item = HapticByOrder(index);
  87. if (item == NULL ) {
  88. SDL_SetError("No such device");
  89. return NULL;
  90. }
  91. return item->name;
  92. }
  93. static SDL_hapticlist_item *
  94. OpenHaptic(SDL_Haptic *haptic, SDL_hapticlist_item *item)
  95. {
  96. if (item == NULL ) {
  97. SDL_SetError("No such device");
  98. return NULL;
  99. }
  100. if (item->haptic != NULL) {
  101. SDL_SetError("Haptic already opened");
  102. return NULL;
  103. }
  104. haptic->hwdata = (struct haptic_hwdata *)item;
  105. item->haptic = haptic;
  106. haptic->supported = SDL_HAPTIC_LEFTRIGHT;
  107. haptic->neffects = 1;
  108. haptic->nplaying = haptic->neffects;
  109. haptic->effects = (struct haptic_effect *)SDL_malloc (sizeof (struct haptic_effect) * haptic->neffects);
  110. if (haptic->effects == NULL) {
  111. SDL_OutOfMemory();
  112. return NULL;
  113. }
  114. SDL_memset(haptic->effects, 0, sizeof (struct haptic_effect) * haptic->neffects);
  115. return item;
  116. }
  117. static SDL_hapticlist_item *
  118. OpenHapticByOrder(SDL_Haptic *haptic, int index)
  119. {
  120. return OpenHaptic (haptic, HapticByOrder(index));
  121. }
  122. static SDL_hapticlist_item *
  123. OpenHapticByDevId(SDL_Haptic *haptic, int device_id)
  124. {
  125. return OpenHaptic (haptic, HapticByDevId(device_id));
  126. }
  127. int
  128. SDL_SYS_HapticOpen(SDL_Haptic *haptic)
  129. {
  130. return (OpenHapticByOrder(haptic, haptic->index) == NULL ? -1 : 0);
  131. }
  132. int
  133. SDL_SYS_HapticMouse(void)
  134. {
  135. return 0;
  136. }
  137. int
  138. SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick)
  139. {
  140. SDL_hapticlist_item *item;
  141. item = HapticByDevId(((joystick_hwdata *)joystick->hwdata)->device_id);
  142. return (item != NULL) ? 1 : 0;
  143. }
  144. int
  145. SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
  146. {
  147. return (OpenHapticByDevId(haptic, ((joystick_hwdata *)joystick->hwdata)->device_id) == NULL ? -1 : 0);
  148. }
  149. int
  150. SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
  151. {
  152. return (((SDL_hapticlist_item *)haptic->hwdata)->device_id == ((joystick_hwdata *)joystick->hwdata)->device_id ? 1 : 0);
  153. }
  154. void
  155. SDL_SYS_HapticClose(SDL_Haptic * haptic)
  156. {
  157. ((SDL_hapticlist_item *)haptic->hwdata)->haptic = NULL;
  158. haptic->hwdata = NULL;
  159. return;
  160. }
  161. void
  162. SDL_SYS_HapticQuit(void)
  163. {
  164. /* We don't have any way to scan for joysticks (and their vibrators) at init, so don't wipe the list
  165. * of joysticks here in case this is a reinit.
  166. */
  167. #if 0
  168. SDL_hapticlist_item *item = NULL;
  169. SDL_hapticlist_item *next = NULL;
  170. for (item = SDL_hapticlist; item; item = next) {
  171. next = item->next;
  172. SDL_free(item);
  173. }
  174. SDL_hapticlist = SDL_hapticlist_tail = NULL;
  175. numhaptics = 0;
  176. return;
  177. #endif
  178. }
  179. int
  180. SDL_SYS_HapticNewEffect(SDL_Haptic * haptic,
  181. struct haptic_effect *effect, SDL_HapticEffect * base)
  182. {
  183. return 0;
  184. }
  185. int
  186. SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
  187. struct haptic_effect *effect,
  188. SDL_HapticEffect * data)
  189. {
  190. return 0;
  191. }
  192. int
  193. SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
  194. Uint32 iterations)
  195. {
  196. float large = effect->effect.leftright.large_magnitude / 32767.0f;
  197. float small = effect->effect.leftright.small_magnitude / 32767.0f;
  198. float total = (large * 0.6f) + (small * 0.4f);
  199. Android_JNI_HapticRun (((SDL_hapticlist_item *)haptic->hwdata)->device_id, total, effect->effect.leftright.length);
  200. return 0;
  201. }
  202. int
  203. SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
  204. {
  205. Android_JNI_HapticStop (((SDL_hapticlist_item *)haptic->hwdata)->device_id);
  206. return 0;
  207. }
  208. void
  209. SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
  210. {
  211. return;
  212. }
  213. int
  214. SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
  215. struct haptic_effect *effect)
  216. {
  217. return 0;
  218. }
  219. int
  220. SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
  221. {
  222. return 0;
  223. }
  224. int
  225. SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
  226. {
  227. return 0;
  228. }
  229. int
  230. SDL_SYS_HapticPause(SDL_Haptic * haptic)
  231. {
  232. return 0;
  233. }
  234. int
  235. SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
  236. {
  237. return 0;
  238. }
  239. int
  240. SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
  241. {
  242. return 0;
  243. }
  244. int
  245. Android_AddHaptic(int device_id, const char *name)
  246. {
  247. SDL_hapticlist_item *item;
  248. item = (SDL_hapticlist_item *) SDL_calloc(1, sizeof (SDL_hapticlist_item));
  249. if (item == NULL) {
  250. return -1;
  251. }
  252. item->device_id = device_id;
  253. item->name = SDL_strdup (name);
  254. if (item->name == NULL) {
  255. SDL_free (item);
  256. return -1;
  257. }
  258. if (SDL_hapticlist_tail == NULL) {
  259. SDL_hapticlist = SDL_hapticlist_tail = item;
  260. } else {
  261. SDL_hapticlist_tail->next = item;
  262. SDL_hapticlist_tail = item;
  263. }
  264. ++numhaptics;
  265. return numhaptics;
  266. }
  267. int
  268. Android_RemoveHaptic(int device_id)
  269. {
  270. SDL_hapticlist_item *item;
  271. SDL_hapticlist_item *prev = NULL;
  272. for (item = SDL_hapticlist; item != NULL; item = item->next) {
  273. /* found it, remove it. */
  274. if (device_id == item->device_id) {
  275. const int retval = item->haptic ? item->haptic->index : -1;
  276. if (prev != NULL) {
  277. prev->next = item->next;
  278. } else {
  279. SDL_assert(SDL_hapticlist == item);
  280. SDL_hapticlist = item->next;
  281. }
  282. if (item == SDL_hapticlist_tail) {
  283. SDL_hapticlist_tail = prev;
  284. }
  285. /* Need to decrement the haptic count */
  286. --numhaptics;
  287. /* !!! TODO: Send a haptic remove event? */
  288. SDL_free(item->name);
  289. SDL_free(item);
  290. return retval;
  291. }
  292. prev = item;
  293. }
  294. return -1;
  295. }
  296. #endif /* SDL_HAPTIC_ANDROID */
  297. /* vi: set ts=4 sw=4 expandtab: */