SDL_syshaptic.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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_syshaptic_c.h"
  21. #include "../SDL_syshaptic.h"
  22. #include "../../core/android/SDL_android.h"
  23. typedef struct SDL_hapticlist_item
  24. {
  25. SDL_HapticID instance_id;
  26. int device_id;
  27. char *name;
  28. SDL_Haptic *haptic;
  29. struct SDL_hapticlist_item *next;
  30. } SDL_hapticlist_item;
  31. static SDL_hapticlist_item *SDL_hapticlist = NULL;
  32. static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
  33. static int numhaptics = 0;
  34. bool SDL_SYS_HapticInit(void)
  35. {
  36. Android_JNI_PollHapticDevices();
  37. return true;
  38. }
  39. int SDL_SYS_NumHaptics(void)
  40. {
  41. return numhaptics;
  42. }
  43. static SDL_hapticlist_item *HapticByOrder(int index)
  44. {
  45. SDL_hapticlist_item *item = SDL_hapticlist;
  46. if ((index < 0) || (index >= numhaptics)) {
  47. return NULL;
  48. }
  49. while (index > 0) {
  50. SDL_assert(item != NULL);
  51. --index;
  52. item = item->next;
  53. }
  54. return item;
  55. }
  56. static SDL_hapticlist_item *HapticByInstanceID(SDL_HapticID instance_id)
  57. {
  58. SDL_hapticlist_item *item;
  59. for (item = SDL_hapticlist; item; item = item->next) {
  60. if (instance_id == item->instance_id) {
  61. return item;
  62. }
  63. }
  64. return NULL;
  65. }
  66. SDL_HapticID SDL_SYS_HapticInstanceID(int index)
  67. {
  68. SDL_hapticlist_item *item = HapticByOrder(index);
  69. if (item) {
  70. return item->instance_id;
  71. }
  72. return 0;
  73. }
  74. const char *SDL_SYS_HapticName(int index)
  75. {
  76. SDL_hapticlist_item *item = HapticByOrder(index);
  77. if (!item) {
  78. SDL_SetError("No such device");
  79. return NULL;
  80. }
  81. return item->name;
  82. }
  83. static SDL_hapticlist_item *OpenHaptic(SDL_Haptic *haptic, SDL_hapticlist_item *item)
  84. {
  85. if (!item) {
  86. SDL_SetError("No such device");
  87. return NULL;
  88. }
  89. if (item->haptic) {
  90. SDL_SetError("Haptic already opened");
  91. return NULL;
  92. }
  93. haptic->hwdata = (struct haptic_hwdata *)item;
  94. item->haptic = haptic;
  95. haptic->instance_id = item->instance_id;
  96. if (item->name) {
  97. haptic->name = SDL_strdup(item->name);
  98. }
  99. haptic->supported = SDL_HAPTIC_LEFTRIGHT;
  100. haptic->neffects = 1;
  101. haptic->nplaying = haptic->neffects;
  102. haptic->effects = (struct haptic_effect *)SDL_calloc(haptic->neffects, sizeof(struct haptic_effect));
  103. if (!haptic->effects) {
  104. return NULL;
  105. }
  106. return item;
  107. }
  108. static SDL_hapticlist_item *OpenHapticByInstanceID(SDL_Haptic *haptic, SDL_HapticID instance_id)
  109. {
  110. return OpenHaptic(haptic, HapticByInstanceID(instance_id));
  111. }
  112. bool SDL_SYS_HapticOpen(SDL_Haptic *haptic)
  113. {
  114. return OpenHapticByInstanceID(haptic, haptic->instance_id) != NULL;
  115. }
  116. int SDL_SYS_HapticMouse(void)
  117. {
  118. return -1;
  119. }
  120. bool SDL_SYS_JoystickIsHaptic(SDL_Joystick *joystick)
  121. {
  122. return false;
  123. }
  124. bool SDL_SYS_HapticOpenFromJoystick(SDL_Haptic *haptic, SDL_Joystick *joystick)
  125. {
  126. return SDL_Unsupported();
  127. }
  128. bool SDL_SYS_JoystickSameHaptic(SDL_Haptic *haptic, SDL_Joystick *joystick)
  129. {
  130. return false;
  131. }
  132. void SDL_SYS_HapticClose(SDL_Haptic *haptic)
  133. {
  134. ((SDL_hapticlist_item *)haptic->hwdata)->haptic = NULL;
  135. haptic->hwdata = NULL;
  136. }
  137. void SDL_SYS_HapticQuit(void)
  138. {
  139. /* We don't have any way to scan for joysticks (and their vibrators) at init, so don't wipe the list
  140. * of joysticks here in case this is a reinit.
  141. */
  142. #if 0
  143. SDL_hapticlist_item *item = NULL;
  144. SDL_hapticlist_item *next = NULL;
  145. for (item = SDL_hapticlist; item; item = next) {
  146. next = item->next;
  147. SDL_free(item);
  148. }
  149. SDL_hapticlist = SDL_hapticlist_tail = NULL;
  150. numhaptics = 0;
  151. return;
  152. #endif
  153. }
  154. bool SDL_SYS_HapticNewEffect(SDL_Haptic *haptic,
  155. struct haptic_effect *effect, const SDL_HapticEffect *base)
  156. {
  157. return true;
  158. }
  159. bool SDL_SYS_HapticUpdateEffect(SDL_Haptic *haptic,
  160. struct haptic_effect *effect,
  161. const SDL_HapticEffect *data)
  162. {
  163. return true;
  164. }
  165. bool SDL_SYS_HapticRunEffect(SDL_Haptic *haptic, struct haptic_effect *effect,
  166. Uint32 iterations)
  167. {
  168. float large = effect->effect.leftright.large_magnitude / 32767.0f;
  169. float small = effect->effect.leftright.small_magnitude / 32767.0f;
  170. float total = (large * 0.6f) + (small * 0.4f);
  171. Android_JNI_HapticRun(((SDL_hapticlist_item *)haptic->hwdata)->device_id, total, effect->effect.leftright.length);
  172. return true;
  173. }
  174. bool SDL_SYS_HapticStopEffect(SDL_Haptic *haptic, struct haptic_effect *effect)
  175. {
  176. Android_JNI_HapticStop(((SDL_hapticlist_item *)haptic->hwdata)->device_id);
  177. return true;
  178. }
  179. void SDL_SYS_HapticDestroyEffect(SDL_Haptic *haptic, struct haptic_effect *effect)
  180. {
  181. }
  182. int SDL_SYS_HapticGetEffectStatus(SDL_Haptic *haptic, struct haptic_effect *effect)
  183. {
  184. return 0;
  185. }
  186. bool SDL_SYS_HapticSetGain(SDL_Haptic *haptic, int gain)
  187. {
  188. return true;
  189. }
  190. bool SDL_SYS_HapticSetAutocenter(SDL_Haptic *haptic, int autocenter)
  191. {
  192. return true;
  193. }
  194. bool SDL_SYS_HapticPause(SDL_Haptic *haptic)
  195. {
  196. return true;
  197. }
  198. bool SDL_SYS_HapticResume(SDL_Haptic *haptic)
  199. {
  200. return true;
  201. }
  202. bool SDL_SYS_HapticStopAll(SDL_Haptic *haptic)
  203. {
  204. return true;
  205. }
  206. bool Android_AddHaptic(int device_id, const char *name)
  207. {
  208. SDL_hapticlist_item *item;
  209. item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item));
  210. if (!item) {
  211. return false;
  212. }
  213. item->instance_id = SDL_GetNextObjectID();
  214. item->device_id = device_id;
  215. item->name = SDL_strdup(name);
  216. if (!item->name) {
  217. SDL_free(item);
  218. return false;
  219. }
  220. if (!SDL_hapticlist_tail) {
  221. SDL_hapticlist = SDL_hapticlist_tail = item;
  222. } else {
  223. SDL_hapticlist_tail->next = item;
  224. SDL_hapticlist_tail = item;
  225. }
  226. ++numhaptics;
  227. return true;
  228. }
  229. bool Android_RemoveHaptic(int device_id)
  230. {
  231. SDL_hapticlist_item *item;
  232. SDL_hapticlist_item *prev = NULL;
  233. for (item = SDL_hapticlist; item; item = item->next) {
  234. // found it, remove it.
  235. if (device_id == item->device_id) {
  236. const bool result = item->haptic ? true : false;
  237. if (prev) {
  238. prev->next = item->next;
  239. } else {
  240. SDL_assert(SDL_hapticlist == item);
  241. SDL_hapticlist = item->next;
  242. }
  243. if (item == SDL_hapticlist_tail) {
  244. SDL_hapticlist_tail = prev;
  245. }
  246. // Need to decrement the haptic count
  247. --numhaptics;
  248. // !!! TODO: Send a haptic remove event?
  249. SDL_free(item->name);
  250. SDL_free(item);
  251. return result;
  252. }
  253. prev = item;
  254. }
  255. return false;
  256. }
  257. #endif // SDL_HAPTIC_ANDROID