SDL_touch.c 15 KB

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