SDL_syshaptic.c 8.0 KB

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