SDL_touch.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. /* General touch handling code for SDL */
  20. #include "SDL_events.h"
  21. #include "SDL_events_c.h"
  22. #include "../video/SDL_sysvideo.h"
  23. static int SDL_num_touch = 0;
  24. static SDL_Touch **SDL_touchDevices = NULL;
  25. /* for mapping touch events to mice */
  26. #define SYNTHESIZE_TOUCH_TO_MOUSE 1
  27. #if SYNTHESIZE_TOUCH_TO_MOUSE
  28. static SDL_bool finger_touching = SDL_FALSE;
  29. static SDL_FingerID track_fingerid;
  30. static SDL_TouchID track_touchid;
  31. #endif
  32. /* Public functions */
  33. int
  34. SDL_TouchInit(void)
  35. {
  36. return (0);
  37. }
  38. int
  39. SDL_GetNumTouchDevices(void)
  40. {
  41. return SDL_num_touch;
  42. }
  43. SDL_TouchID
  44. SDL_GetTouchDevice(int index)
  45. {
  46. if (index < 0 || index >= SDL_num_touch) {
  47. SDL_SetError("Unknown touch device index %d", index);
  48. return 0;
  49. }
  50. return SDL_touchDevices[index]->id;
  51. }
  52. static int
  53. SDL_GetTouchIndex(SDL_TouchID id)
  54. {
  55. int index;
  56. SDL_Touch *touch;
  57. for (index = 0; index < SDL_num_touch; ++index) {
  58. touch = SDL_touchDevices[index];
  59. if (touch->id == id) {
  60. return index;
  61. }
  62. }
  63. return -1;
  64. }
  65. SDL_Touch *
  66. SDL_GetTouch(SDL_TouchID id)
  67. {
  68. int index = SDL_GetTouchIndex(id);
  69. if (index < 0 || index >= SDL_num_touch) {
  70. if (SDL_GetVideoDevice()->ResetTouch != NULL) {
  71. SDL_SetError("Unknown touch id %d, resetting", (int) id);
  72. (SDL_GetVideoDevice()->ResetTouch)(SDL_GetVideoDevice());
  73. } else {
  74. SDL_SetError("Unknown touch device id %d, cannot reset", (int) id);
  75. }
  76. return NULL;
  77. }
  78. return SDL_touchDevices[index];
  79. }
  80. SDL_TouchDeviceType
  81. SDL_GetTouchDeviceType(SDL_TouchID id)
  82. {
  83. SDL_Touch *touch = SDL_GetTouch(id);
  84. if (touch) {
  85. return touch->type;
  86. }
  87. return SDL_TOUCH_DEVICE_INVALID;
  88. }
  89. static int
  90. SDL_GetFingerIndex(const SDL_Touch * touch, SDL_FingerID fingerid)
  91. {
  92. int index;
  93. for (index = 0; index < touch->num_fingers; ++index) {
  94. if (touch->fingers[index]->id == fingerid) {
  95. return index;
  96. }
  97. }
  98. return -1;
  99. }
  100. static SDL_Finger *
  101. SDL_GetFinger(const SDL_Touch * touch, SDL_FingerID id)
  102. {
  103. int index = SDL_GetFingerIndex(touch, id);
  104. if (index < 0 || index >= touch->num_fingers) {
  105. return NULL;
  106. }
  107. return touch->fingers[index];
  108. }
  109. int
  110. SDL_GetNumTouchFingers(SDL_TouchID touchID)
  111. {
  112. SDL_Touch *touch = SDL_GetTouch(touchID);
  113. if (touch) {
  114. return touch->num_fingers;
  115. }
  116. return 0;
  117. }
  118. SDL_Finger *
  119. SDL_GetTouchFinger(SDL_TouchID touchID, int index)
  120. {
  121. SDL_Touch *touch = SDL_GetTouch(touchID);
  122. if (!touch) {
  123. return NULL;
  124. }
  125. if (index < 0 || index >= touch->num_fingers) {
  126. SDL_SetError("Unknown touch finger");
  127. return NULL;
  128. }
  129. return touch->fingers[index];
  130. }
  131. int
  132. SDL_AddTouch(SDL_TouchID touchID, SDL_TouchDeviceType type, const char *name)
  133. {
  134. SDL_Touch **touchDevices;
  135. int index;
  136. index = SDL_GetTouchIndex(touchID);
  137. if (index >= 0) {
  138. return index;
  139. }
  140. /* Add the touch to the list of touch */
  141. touchDevices = (SDL_Touch **) SDL_realloc(SDL_touchDevices,
  142. (SDL_num_touch + 1) * sizeof(*touchDevices));
  143. if (!touchDevices) {
  144. return SDL_OutOfMemory();
  145. }
  146. SDL_touchDevices = touchDevices;
  147. index = SDL_num_touch;
  148. SDL_touchDevices[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touchDevices[index]));
  149. if (!SDL_touchDevices[index]) {
  150. return SDL_OutOfMemory();
  151. }
  152. /* Added touch to list */
  153. ++SDL_num_touch;
  154. /* we're setting the touch properties */
  155. SDL_touchDevices[index]->id = touchID;
  156. SDL_touchDevices[index]->type = type;
  157. SDL_touchDevices[index]->num_fingers = 0;
  158. SDL_touchDevices[index]->max_fingers = 0;
  159. SDL_touchDevices[index]->fingers = NULL;
  160. /* Record this touch device for gestures */
  161. /* We could do this on the fly in the gesture code if we wanted */
  162. SDL_GestureAddTouch(touchID);
  163. return index;
  164. }
  165. static int
  166. SDL_AddFinger(SDL_Touch *touch, SDL_FingerID fingerid, float x, float y, float pressure)
  167. {
  168. SDL_Finger *finger;
  169. if (touch->num_fingers == touch->max_fingers) {
  170. SDL_Finger **new_fingers;
  171. new_fingers = (SDL_Finger **)SDL_realloc(touch->fingers, (touch->max_fingers+1)*sizeof(*touch->fingers));
  172. if (!new_fingers) {
  173. return SDL_OutOfMemory();
  174. }
  175. touch->fingers = new_fingers;
  176. touch->fingers[touch->max_fingers] = (SDL_Finger *)SDL_malloc(sizeof(*finger));
  177. if (!touch->fingers[touch->max_fingers]) {
  178. return SDL_OutOfMemory();
  179. }
  180. touch->max_fingers++;
  181. }
  182. finger = touch->fingers[touch->num_fingers++];
  183. finger->id = fingerid;
  184. finger->x = x;
  185. finger->y = y;
  186. finger->pressure = pressure;
  187. return 0;
  188. }
  189. static int
  190. SDL_DelFinger(SDL_Touch* touch, SDL_FingerID fingerid)
  191. {
  192. SDL_Finger *temp;
  193. int index = SDL_GetFingerIndex(touch, fingerid);
  194. if (index < 0) {
  195. return -1;
  196. }
  197. touch->num_fingers--;
  198. temp = touch->fingers[index];
  199. touch->fingers[index] = touch->fingers[touch->num_fingers];
  200. touch->fingers[touch->num_fingers] = temp;
  201. return 0;
  202. }
  203. int
  204. SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid, SDL_Window * window,
  205. SDL_bool down, float x, float y, float pressure)
  206. {
  207. int posted;
  208. SDL_Finger *finger;
  209. SDL_Mouse *mouse;
  210. SDL_Touch* touch = SDL_GetTouch(id);
  211. if (!touch) {
  212. return -1;
  213. }
  214. mouse = SDL_GetMouse();
  215. #if SYNTHESIZE_TOUCH_TO_MOUSE
  216. /* SDL_HINT_TOUCH_MOUSE_EVENTS: controlling whether touch events should generate synthetic mouse events */
  217. {
  218. if (mouse->touch_mouse_events) {
  219. /* FIXME: maybe we should only restrict to a few SDL_TouchDeviceType */
  220. if (id != SDL_MOUSE_TOUCHID) {
  221. if (window) {
  222. if (down) {
  223. if (finger_touching == SDL_FALSE) {
  224. int pos_x = (int)(x * (float)window->w);
  225. int pos_y = (int)(y * (float)window->h);
  226. if (pos_x < 0) pos_x = 0;
  227. if (pos_x > window->w - 1) pos_x = window->w - 1;
  228. if (pos_y < 0) pos_y = 0;
  229. if (pos_y > window->h - 1) pos_y = window->h - 1;
  230. SDL_SendMouseMotion(window, SDL_TOUCH_MOUSEID, 0, pos_x, pos_y);
  231. SDL_SendMouseButton(window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
  232. }
  233. } else {
  234. if (finger_touching == SDL_TRUE && track_touchid == id && track_fingerid == fingerid) {
  235. SDL_SendMouseButton(window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
  236. }
  237. }
  238. }
  239. if (down) {
  240. if (finger_touching == SDL_FALSE) {
  241. finger_touching = SDL_TRUE;
  242. track_touchid = id;
  243. track_fingerid = fingerid;
  244. }
  245. } else {
  246. if (finger_touching == SDL_TRUE && track_touchid == id && track_fingerid == fingerid) {
  247. finger_touching = SDL_FALSE;
  248. }
  249. }
  250. }
  251. }
  252. }
  253. #endif
  254. /* SDL_HINT_MOUSE_TOUCH_EVENTS: if not set, discard synthetic touch events coming from platform layer */
  255. if (mouse->mouse_touch_events == 0) {
  256. if (id == SDL_MOUSE_TOUCHID) {
  257. return 0;
  258. }
  259. }
  260. finger = SDL_GetFinger(touch, fingerid);
  261. if (down) {
  262. if (finger) {
  263. /* This finger is already down.
  264. Assume the finger-up for the previous touch was lost, and send it. */
  265. SDL_SendTouch(id, fingerid, window, SDL_FALSE, x, y, pressure);
  266. }
  267. if (SDL_AddFinger(touch, fingerid, x, y, pressure) < 0) {
  268. return 0;
  269. }
  270. posted = 0;
  271. if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
  272. SDL_Event event;
  273. event.tfinger.type = SDL_FINGERDOWN;
  274. event.tfinger.touchId = id;
  275. event.tfinger.fingerId = fingerid;
  276. event.tfinger.x = x;
  277. event.tfinger.y = y;
  278. event.tfinger.dx = 0;
  279. event.tfinger.dy = 0;
  280. event.tfinger.pressure = pressure;
  281. event.tfinger.windowID = window ? SDL_GetWindowID(window) : 0;
  282. posted = (SDL_PushEvent(&event) > 0);
  283. }
  284. } else {
  285. if (!finger) {
  286. /* This finger is already up */
  287. return 0;
  288. }
  289. posted = 0;
  290. if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
  291. SDL_Event event;
  292. event.tfinger.type = SDL_FINGERUP;
  293. event.tfinger.touchId = id;
  294. event.tfinger.fingerId = fingerid;
  295. /* I don't trust the coordinates passed on fingerUp */
  296. event.tfinger.x = finger->x;
  297. event.tfinger.y = finger->y;
  298. event.tfinger.dx = 0;
  299. event.tfinger.dy = 0;
  300. event.tfinger.pressure = pressure;
  301. event.tfinger.windowID = window ? SDL_GetWindowID(window) : 0;
  302. posted = (SDL_PushEvent(&event) > 0);
  303. }
  304. SDL_DelFinger(touch, fingerid);
  305. }
  306. return posted;
  307. }
  308. int
  309. SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, SDL_Window * window,
  310. float x, float y, float pressure)
  311. {
  312. SDL_Touch *touch;
  313. SDL_Finger *finger;
  314. SDL_Mouse *mouse;
  315. int posted;
  316. float xrel, yrel, prel;
  317. touch = SDL_GetTouch(id);
  318. if (!touch) {
  319. return -1;
  320. }
  321. mouse = SDL_GetMouse();
  322. #if SYNTHESIZE_TOUCH_TO_MOUSE
  323. /* SDL_HINT_TOUCH_MOUSE_EVENTS: controlling whether touch events should generate synthetic mouse events */
  324. {
  325. if (mouse->touch_mouse_events) {
  326. if (id != SDL_MOUSE_TOUCHID) {
  327. if (window) {
  328. if (finger_touching == SDL_TRUE && track_touchid == id && track_fingerid == fingerid) {
  329. int pos_x = (int)(x * (float)window->w);
  330. int pos_y = (int)(y * (float)window->h);
  331. if (pos_x < 0) pos_x = 0;
  332. if (pos_x > window->w - 1) pos_x = window->w - 1;
  333. if (pos_y < 0) pos_y = 0;
  334. if (pos_y > window->h - 1) pos_y = window->h - 1;
  335. SDL_SendMouseMotion(window, SDL_TOUCH_MOUSEID, 0, pos_x, pos_y);
  336. }
  337. }
  338. }
  339. }
  340. }
  341. #endif
  342. /* SDL_HINT_MOUSE_TOUCH_EVENTS: if not set, discard synthetic touch events coming from platform layer */
  343. if (mouse->mouse_touch_events == 0) {
  344. if (id == SDL_MOUSE_TOUCHID) {
  345. return 0;
  346. }
  347. }
  348. finger = SDL_GetFinger(touch,fingerid);
  349. if (!finger) {
  350. return SDL_SendTouch(id, fingerid, window, SDL_TRUE, x, y, pressure);
  351. }
  352. xrel = x - finger->x;
  353. yrel = y - finger->y;
  354. prel = pressure - finger->pressure;
  355. /* Drop events that don't change state */
  356. if (xrel == 0.0f && yrel == 0.0f && prel == 0.0f) {
  357. #if 0
  358. printf("Touch event didn't change state - dropped!\n");
  359. #endif
  360. return 0;
  361. }
  362. /* Update internal touch coordinates */
  363. finger->x = x;
  364. finger->y = y;
  365. finger->pressure = pressure;
  366. /* Post the event, if desired */
  367. posted = 0;
  368. if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
  369. SDL_Event event;
  370. event.tfinger.type = SDL_FINGERMOTION;
  371. event.tfinger.touchId = id;
  372. event.tfinger.fingerId = fingerid;
  373. event.tfinger.x = x;
  374. event.tfinger.y = y;
  375. event.tfinger.dx = xrel;
  376. event.tfinger.dy = yrel;
  377. event.tfinger.pressure = pressure;
  378. event.tfinger.windowID = window ? SDL_GetWindowID(window) : 0;
  379. posted = (SDL_PushEvent(&event) > 0);
  380. }
  381. return posted;
  382. }
  383. void
  384. SDL_DelTouch(SDL_TouchID id)
  385. {
  386. int i;
  387. int index = SDL_GetTouchIndex(id);
  388. SDL_Touch *touch = SDL_GetTouch(id);
  389. if (!touch) {
  390. return;
  391. }
  392. for (i = 0; i < touch->max_fingers; ++i) {
  393. SDL_free(touch->fingers[i]);
  394. }
  395. SDL_free(touch->fingers);
  396. SDL_free(touch);
  397. SDL_num_touch--;
  398. SDL_touchDevices[index] = SDL_touchDevices[SDL_num_touch];
  399. /* Delete this touch device for gestures */
  400. SDL_GestureDelTouch(id);
  401. }
  402. void
  403. SDL_TouchQuit(void)
  404. {
  405. int i;
  406. for (i = SDL_num_touch; i--; ) {
  407. SDL_DelTouch(SDL_touchDevices[i]->id);
  408. }
  409. SDL_assert(SDL_num_touch == 0);
  410. SDL_free(SDL_touchDevices);
  411. SDL_touchDevices = NULL;
  412. SDL_GestureQuit();
  413. }
  414. /* vi: set ts=4 sw=4 expandtab: */