SDL_touch.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. /* General touch handling code for SDL */
  20. #include "SDL_assert.h"
  21. #include "SDL_events.h"
  22. #include "SDL_events_c.h"
  23. static int SDL_num_touch = 0;
  24. static SDL_Touch **SDL_touchDevices = NULL;
  25. /* Public functions */
  26. int
  27. SDL_TouchInit(void)
  28. {
  29. return (0);
  30. }
  31. int
  32. SDL_GetNumTouchDevices(void)
  33. {
  34. return SDL_num_touch;
  35. }
  36. SDL_TouchID
  37. SDL_GetTouchDevice(int index)
  38. {
  39. if (index < 0 || index >= SDL_num_touch) {
  40. SDL_SetError("Unknown touch device");
  41. return 0;
  42. }
  43. return SDL_touchDevices[index]->id;
  44. }
  45. static int
  46. SDL_GetTouchIndex(SDL_TouchID id)
  47. {
  48. int index;
  49. SDL_Touch *touch;
  50. for (index = 0; index < SDL_num_touch; ++index) {
  51. touch = SDL_touchDevices[index];
  52. if (touch->id == id) {
  53. return index;
  54. }
  55. }
  56. return -1;
  57. }
  58. SDL_Touch *
  59. SDL_GetTouch(SDL_TouchID id)
  60. {
  61. int index = SDL_GetTouchIndex(id);
  62. if (index < 0 || index >= SDL_num_touch) {
  63. SDL_SetError("Unknown touch device");
  64. return NULL;
  65. }
  66. return SDL_touchDevices[index];
  67. }
  68. static int
  69. SDL_GetFingerIndex(const SDL_Touch * touch, SDL_FingerID fingerid)
  70. {
  71. int index;
  72. for (index = 0; index < touch->num_fingers; ++index) {
  73. if (touch->fingers[index]->id == fingerid) {
  74. return index;
  75. }
  76. }
  77. return -1;
  78. }
  79. static SDL_Finger *
  80. SDL_GetFinger(const SDL_Touch * touch, SDL_FingerID id)
  81. {
  82. int index = SDL_GetFingerIndex(touch, id);
  83. if (index < 0 || index >= touch->num_fingers) {
  84. return NULL;
  85. }
  86. return touch->fingers[index];
  87. }
  88. int
  89. SDL_GetNumTouchFingers(SDL_TouchID touchID)
  90. {
  91. SDL_Touch *touch = SDL_GetTouch(touchID);
  92. if (touch) {
  93. return touch->num_fingers;
  94. }
  95. return 0;
  96. }
  97. SDL_Finger *
  98. SDL_GetTouchFinger(SDL_TouchID touchID, int index)
  99. {
  100. SDL_Touch *touch = SDL_GetTouch(touchID);
  101. if (!touch) {
  102. return NULL;
  103. }
  104. if (index < 0 || index >= touch->num_fingers) {
  105. SDL_SetError("Unknown touch finger");
  106. return NULL;
  107. }
  108. return touch->fingers[index];
  109. }
  110. int
  111. SDL_AddTouch(SDL_TouchID touchID, const char *name)
  112. {
  113. SDL_Touch **touchDevices;
  114. int index;
  115. index = SDL_GetTouchIndex(touchID);
  116. if (index >= 0) {
  117. return index;
  118. }
  119. /* Add the touch to the list of touch */
  120. touchDevices = (SDL_Touch **) SDL_realloc(SDL_touchDevices,
  121. (SDL_num_touch + 1) * sizeof(*touchDevices));
  122. if (!touchDevices) {
  123. return SDL_OutOfMemory();
  124. }
  125. SDL_touchDevices = touchDevices;
  126. index = SDL_num_touch;
  127. SDL_touchDevices[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touchDevices[index]));
  128. if (!SDL_touchDevices[index]) {
  129. return SDL_OutOfMemory();
  130. }
  131. /* Added touch to list */
  132. ++SDL_num_touch;
  133. /* we're setting the touch properties */
  134. SDL_touchDevices[index]->id = touchID;
  135. SDL_touchDevices[index]->num_fingers = 0;
  136. SDL_touchDevices[index]->max_fingers = 0;
  137. SDL_touchDevices[index]->fingers = NULL;
  138. /* Record this touch device for gestures */
  139. /* We could do this on the fly in the gesture code if we wanted */
  140. SDL_GestureAddTouch(touchID);
  141. return index;
  142. }
  143. static int
  144. SDL_AddFinger(SDL_Touch *touch, SDL_FingerID fingerid, float x, float y, float pressure)
  145. {
  146. SDL_Finger *finger;
  147. if (touch->num_fingers == touch->max_fingers) {
  148. SDL_Finger **new_fingers;
  149. new_fingers = (SDL_Finger **)SDL_realloc(touch->fingers, (touch->max_fingers+1)*sizeof(*touch->fingers));
  150. if (!new_fingers) {
  151. return SDL_OutOfMemory();
  152. }
  153. touch->fingers = new_fingers;
  154. touch->fingers[touch->max_fingers] = (SDL_Finger *)SDL_malloc(sizeof(*finger));
  155. if (!touch->fingers[touch->max_fingers]) {
  156. return SDL_OutOfMemory();
  157. }
  158. touch->max_fingers++;
  159. }
  160. finger = touch->fingers[touch->num_fingers++];
  161. finger->id = fingerid;
  162. finger->x = x;
  163. finger->y = y;
  164. finger->pressure = pressure;
  165. return 0;
  166. }
  167. static int
  168. SDL_DelFinger(SDL_Touch* touch, SDL_FingerID fingerid)
  169. {
  170. SDL_Finger *temp;
  171. int index = SDL_GetFingerIndex(touch, fingerid);
  172. if (index < 0) {
  173. return -1;
  174. }
  175. touch->num_fingers--;
  176. temp = touch->fingers[index];
  177. touch->fingers[index] = touch->fingers[touch->num_fingers];
  178. touch->fingers[touch->num_fingers] = temp;
  179. return 0;
  180. }
  181. int
  182. SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
  183. SDL_bool down, float x, float y, float pressure)
  184. {
  185. int posted;
  186. SDL_Finger *finger;
  187. SDL_Touch* touch = SDL_GetTouch(id);
  188. if (!touch) {
  189. return -1;
  190. }
  191. finger = SDL_GetFinger(touch, fingerid);
  192. if (down) {
  193. if (finger) {
  194. /* This finger is already down */
  195. return 0;
  196. }
  197. if (SDL_AddFinger(touch, fingerid, x, y, pressure) < 0) {
  198. return 0;
  199. }
  200. posted = 0;
  201. if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
  202. SDL_Event event;
  203. event.tfinger.type = SDL_FINGERDOWN;
  204. event.tfinger.touchId = id;
  205. event.tfinger.fingerId = fingerid;
  206. event.tfinger.x = x;
  207. event.tfinger.y = y;
  208. event.tfinger.dx = 0;
  209. event.tfinger.dy = 0;
  210. event.tfinger.pressure = pressure;
  211. posted = (SDL_PushEvent(&event) > 0);
  212. }
  213. } else {
  214. if (!finger) {
  215. /* This finger is already up */
  216. return 0;
  217. }
  218. posted = 0;
  219. if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
  220. SDL_Event event;
  221. event.tfinger.type = SDL_FINGERUP;
  222. event.tfinger.touchId = id;
  223. event.tfinger.fingerId = fingerid;
  224. /* I don't trust the coordinates passed on fingerUp */
  225. event.tfinger.x = finger->x;
  226. event.tfinger.y = finger->y;
  227. event.tfinger.dx = 0;
  228. event.tfinger.dy = 0;
  229. event.tfinger.pressure = pressure;
  230. posted = (SDL_PushEvent(&event) > 0);
  231. }
  232. SDL_DelFinger(touch, fingerid);
  233. }
  234. return posted;
  235. }
  236. int
  237. SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
  238. float x, float y, float pressure)
  239. {
  240. SDL_Touch *touch;
  241. SDL_Finger *finger;
  242. int posted;
  243. float xrel, yrel, prel;
  244. touch = SDL_GetTouch(id);
  245. if (!touch) {
  246. return -1;
  247. }
  248. finger = SDL_GetFinger(touch,fingerid);
  249. if (!finger) {
  250. return SDL_SendTouch(id, fingerid, SDL_TRUE, x, y, pressure);
  251. }
  252. xrel = x - finger->x;
  253. yrel = y - finger->y;
  254. prel = pressure - finger->pressure;
  255. /* Drop events that don't change state */
  256. if (!xrel && !yrel && !prel) {
  257. #if 0
  258. printf("Touch event didn't change state - dropped!\n");
  259. #endif
  260. return 0;
  261. }
  262. /* Update internal touch coordinates */
  263. finger->x = x;
  264. finger->y = y;
  265. finger->pressure = pressure;
  266. /* Post the event, if desired */
  267. posted = 0;
  268. if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
  269. SDL_Event event;
  270. event.tfinger.type = SDL_FINGERMOTION;
  271. event.tfinger.touchId = id;
  272. event.tfinger.fingerId = fingerid;
  273. event.tfinger.x = x;
  274. event.tfinger.y = y;
  275. event.tfinger.dx = xrel;
  276. event.tfinger.dy = yrel;
  277. event.tfinger.pressure = pressure;
  278. posted = (SDL_PushEvent(&event) > 0);
  279. }
  280. return posted;
  281. }
  282. void
  283. SDL_DelTouch(SDL_TouchID id)
  284. {
  285. int i;
  286. int index = SDL_GetTouchIndex(id);
  287. SDL_Touch *touch = SDL_GetTouch(id);
  288. if (!touch) {
  289. return;
  290. }
  291. for (i = 0; i < touch->max_fingers; ++i) {
  292. SDL_free(touch->fingers[i]);
  293. }
  294. SDL_free(touch->fingers);
  295. SDL_free(touch);
  296. SDL_num_touch--;
  297. SDL_touchDevices[index] = SDL_touchDevices[SDL_num_touch];
  298. }
  299. void
  300. SDL_TouchQuit(void)
  301. {
  302. int i;
  303. for (i = SDL_num_touch; i--; ) {
  304. SDL_DelTouch(SDL_touchDevices[i]->id);
  305. }
  306. SDL_assert(SDL_num_touch == 0);
  307. SDL_free(SDL_touchDevices);
  308. SDL_touchDevices = NULL;
  309. }
  310. /* vi: set ts=4 sw=4 expandtab: */