SDL_touch.c 14 KB

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