SDL_mouse.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  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 mouse handling code for SDL
  20. #include "../SDL_hints_c.h"
  21. #include "../video/SDL_sysvideo.h"
  22. #include "SDL_events_c.h"
  23. #include "SDL_mouse_c.h"
  24. #if defined(SDL_PLATFORM_WINDOWS)
  25. #include "../core/windows/SDL_windows.h" // For GetDoubleClickTime()
  26. #endif
  27. // #define DEBUG_MOUSE
  28. #define WARP_EMULATION_THRESHOLD_NS SDL_MS_TO_NS(30)
  29. typedef struct SDL_MouseInstance
  30. {
  31. SDL_MouseID instance_id;
  32. char *name;
  33. } SDL_MouseInstance;
  34. // The mouse state
  35. static SDL_Mouse SDL_mouse;
  36. static int SDL_mouse_count;
  37. static SDL_MouseInstance *SDL_mice;
  38. // for mapping mouse events to touch
  39. static bool track_mouse_down = false;
  40. static void SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, bool relative, float x, float y);
  41. static void SDLCALL SDL_MouseDoubleClickTimeChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  42. {
  43. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  44. if (hint && *hint) {
  45. mouse->double_click_time = SDL_atoi(hint);
  46. } else {
  47. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  48. mouse->double_click_time = GetDoubleClickTime();
  49. #else
  50. mouse->double_click_time = 500;
  51. #endif
  52. }
  53. }
  54. static void SDLCALL SDL_MouseDoubleClickRadiusChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  55. {
  56. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  57. if (hint && *hint) {
  58. mouse->double_click_radius = SDL_atoi(hint);
  59. } else {
  60. mouse->double_click_radius = 32; // 32 pixels seems about right for touch interfaces
  61. }
  62. }
  63. static void SDLCALL SDL_MouseNormalSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  64. {
  65. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  66. if (hint && *hint) {
  67. mouse->enable_normal_speed_scale = true;
  68. mouse->normal_speed_scale = (float)SDL_atof(hint);
  69. } else {
  70. mouse->enable_normal_speed_scale = false;
  71. mouse->normal_speed_scale = 1.0f;
  72. }
  73. }
  74. static void SDLCALL SDL_MouseRelativeSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  75. {
  76. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  77. if (hint && *hint) {
  78. mouse->enable_relative_speed_scale = true;
  79. mouse->relative_speed_scale = (float)SDL_atof(hint);
  80. } else {
  81. mouse->enable_relative_speed_scale = false;
  82. mouse->relative_speed_scale = 1.0f;
  83. }
  84. }
  85. static void SDLCALL SDL_MouseRelativeModeCenterChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  86. {
  87. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  88. mouse->relative_mode_center = SDL_GetStringBoolean(hint, true);
  89. }
  90. static void SDLCALL SDL_MouseRelativeSystemScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  91. {
  92. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  93. mouse->enable_relative_system_scale = SDL_GetStringBoolean(hint, false);
  94. }
  95. static void SDLCALL SDL_MouseWarpEmulationChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  96. {
  97. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  98. mouse->warp_emulation_hint = SDL_GetStringBoolean(hint, true);
  99. if (!mouse->warp_emulation_hint && mouse->warp_emulation_active) {
  100. SDL_SetRelativeMouseMode(false);
  101. mouse->warp_emulation_active = false;
  102. }
  103. }
  104. static void SDLCALL SDL_TouchMouseEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  105. {
  106. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  107. mouse->touch_mouse_events = SDL_GetStringBoolean(hint, true);
  108. }
  109. #ifdef SDL_PLATFORM_VITA
  110. static void SDLCALL SDL_VitaTouchMouseDeviceChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  111. {
  112. Uint8 vita_touch_mouse_device = 1;
  113. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  114. if (hint) {
  115. switch (*hint) {
  116. case '0':
  117. vita_touch_mouse_device = 1;
  118. break;
  119. case '1':
  120. vita_touch_mouse_device = 2;
  121. break;
  122. case '2':
  123. vita_touch_mouse_device = 3;
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. mouse->vita_touch_mouse_device = vita_touch_mouse_device;
  130. }
  131. #endif
  132. static void SDLCALL SDL_MouseTouchEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  133. {
  134. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  135. bool default_value;
  136. #if defined(SDL_PLATFORM_ANDROID) || (defined(SDL_PLATFORM_IOS) && !defined(SDL_PLATFORM_TVOS))
  137. default_value = true;
  138. #else
  139. default_value = false;
  140. #endif
  141. mouse->mouse_touch_events = SDL_GetStringBoolean(hint, default_value);
  142. if (mouse->mouse_touch_events) {
  143. if (!mouse->added_mouse_touch_device) {
  144. SDL_AddTouch(SDL_MOUSE_TOUCHID, SDL_TOUCH_DEVICE_DIRECT, "mouse_input");
  145. mouse->added_mouse_touch_device = true;
  146. }
  147. } else {
  148. if (mouse->added_mouse_touch_device) {
  149. SDL_DelTouch(SDL_MOUSE_TOUCHID);
  150. mouse->added_mouse_touch_device = false;
  151. }
  152. }
  153. }
  154. static void SDLCALL SDL_PenMouseEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  155. {
  156. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  157. mouse->pen_mouse_events = SDL_GetStringBoolean(hint, true);
  158. }
  159. static void SDLCALL SDL_PenTouchEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  160. {
  161. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  162. mouse->pen_touch_events = SDL_GetStringBoolean(hint, true);
  163. if (mouse->pen_touch_events) {
  164. if (!mouse->added_pen_touch_device) {
  165. SDL_AddTouch(SDL_PEN_TOUCHID, SDL_TOUCH_DEVICE_DIRECT, "pen_input");
  166. mouse->added_pen_touch_device = true;
  167. }
  168. } else {
  169. if (mouse->added_pen_touch_device) {
  170. SDL_DelTouch(SDL_PEN_TOUCHID);
  171. mouse->added_pen_touch_device = false;
  172. }
  173. }
  174. }
  175. static void SDLCALL SDL_MouseAutoCaptureChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  176. {
  177. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  178. bool auto_capture = SDL_GetStringBoolean(hint, true);
  179. if (auto_capture != mouse->auto_capture) {
  180. mouse->auto_capture = auto_capture;
  181. SDL_UpdateMouseCapture(false);
  182. }
  183. }
  184. static void SDLCALL SDL_MouseRelativeWarpMotionChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  185. {
  186. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  187. mouse->relative_mode_warp_motion = SDL_GetStringBoolean(hint, false);
  188. }
  189. static void SDLCALL SDL_MouseRelativeCursorVisibleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  190. {
  191. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  192. mouse->relative_mode_hide_cursor = !(SDL_GetStringBoolean(hint, false));
  193. SDL_RedrawCursor(); // Update cursor visibility
  194. }
  195. static void SDLCALL SDL_MouseIntegerModeChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  196. {
  197. SDL_Mouse *mouse = (SDL_Mouse *)userdata;
  198. if (hint && *hint) {
  199. mouse->integer_mode_flags = (Uint8)SDL_atoi(hint);
  200. } else {
  201. mouse->integer_mode_flags = 0;
  202. }
  203. }
  204. // Public functions
  205. bool SDL_PreInitMouse(void)
  206. {
  207. SDL_Mouse *mouse = SDL_GetMouse();
  208. SDL_zerop(mouse);
  209. SDL_AddHintCallback(SDL_HINT_MOUSE_DOUBLE_CLICK_TIME,
  210. SDL_MouseDoubleClickTimeChanged, mouse);
  211. SDL_AddHintCallback(SDL_HINT_MOUSE_DOUBLE_CLICK_RADIUS,
  212. SDL_MouseDoubleClickRadiusChanged, mouse);
  213. SDL_AddHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
  214. SDL_MouseNormalSpeedScaleChanged, mouse);
  215. SDL_AddHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
  216. SDL_MouseRelativeSpeedScaleChanged, mouse);
  217. SDL_AddHintCallback(SDL_HINT_MOUSE_RELATIVE_SYSTEM_SCALE,
  218. SDL_MouseRelativeSystemScaleChanged, mouse);
  219. SDL_AddHintCallback(SDL_HINT_MOUSE_RELATIVE_MODE_CENTER,
  220. SDL_MouseRelativeModeCenterChanged, mouse);
  221. SDL_AddHintCallback(SDL_HINT_MOUSE_EMULATE_WARP_WITH_RELATIVE,
  222. SDL_MouseWarpEmulationChanged, mouse);
  223. SDL_AddHintCallback(SDL_HINT_TOUCH_MOUSE_EVENTS,
  224. SDL_TouchMouseEventsChanged, mouse);
  225. #ifdef SDL_PLATFORM_VITA
  226. SDL_AddHintCallback(SDL_HINT_VITA_TOUCH_MOUSE_DEVICE,
  227. SDL_VitaTouchMouseDeviceChanged, mouse);
  228. #endif
  229. SDL_AddHintCallback(SDL_HINT_MOUSE_TOUCH_EVENTS,
  230. SDL_MouseTouchEventsChanged, mouse);
  231. SDL_AddHintCallback(SDL_HINT_PEN_MOUSE_EVENTS,
  232. SDL_PenMouseEventsChanged, mouse);
  233. SDL_AddHintCallback(SDL_HINT_PEN_TOUCH_EVENTS,
  234. SDL_PenTouchEventsChanged, mouse);
  235. SDL_AddHintCallback(SDL_HINT_MOUSE_AUTO_CAPTURE,
  236. SDL_MouseAutoCaptureChanged, mouse);
  237. SDL_AddHintCallback(SDL_HINT_MOUSE_RELATIVE_WARP_MOTION,
  238. SDL_MouseRelativeWarpMotionChanged, mouse);
  239. SDL_AddHintCallback(SDL_HINT_MOUSE_RELATIVE_CURSOR_VISIBLE,
  240. SDL_MouseRelativeCursorVisibleChanged, mouse);
  241. SDL_AddHintCallback("SDL_MOUSE_INTEGER_MODE",
  242. SDL_MouseIntegerModeChanged, mouse);
  243. mouse->was_touch_mouse_events = false; // no touch to mouse movement event pending
  244. mouse->cursor_visible = true;
  245. return true;
  246. }
  247. void SDL_PostInitMouse(void)
  248. {
  249. SDL_Mouse *mouse = SDL_GetMouse();
  250. /* Create a dummy mouse cursor for video backends that don't support true cursors,
  251. * so that mouse grab and focus functionality will work.
  252. */
  253. if (!mouse->def_cursor) {
  254. SDL_Surface *surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_ARGB8888);
  255. if (surface) {
  256. SDL_memset(surface->pixels, 0, (size_t)surface->h * surface->pitch);
  257. SDL_SetDefaultCursor(SDL_CreateColorCursor(surface, 0, 0));
  258. SDL_DestroySurface(surface);
  259. }
  260. }
  261. }
  262. bool SDL_IsMouse(Uint16 vendor, Uint16 product)
  263. {
  264. // Eventually we'll have a blacklist of devices that enumerate as mice but aren't really
  265. return true;
  266. }
  267. static int SDL_GetMouseIndex(SDL_MouseID mouseID)
  268. {
  269. for (int i = 0; i < SDL_mouse_count; ++i) {
  270. if (mouseID == SDL_mice[i].instance_id) {
  271. return i;
  272. }
  273. }
  274. return -1;
  275. }
  276. void SDL_AddMouse(SDL_MouseID mouseID, const char *name, bool send_event)
  277. {
  278. int mouse_index = SDL_GetMouseIndex(mouseID);
  279. if (mouse_index >= 0) {
  280. // We already know about this mouse
  281. return;
  282. }
  283. SDL_assert(mouseID != 0);
  284. SDL_MouseInstance *mice = (SDL_MouseInstance *)SDL_realloc(SDL_mice, (SDL_mouse_count + 1) * sizeof(*mice));
  285. if (!mice) {
  286. return;
  287. }
  288. SDL_MouseInstance *instance = &mice[SDL_mouse_count];
  289. instance->instance_id = mouseID;
  290. instance->name = SDL_strdup(name ? name : "");
  291. SDL_mice = mice;
  292. ++SDL_mouse_count;
  293. if (send_event) {
  294. SDL_Event event;
  295. SDL_zero(event);
  296. event.type = SDL_EVENT_MOUSE_ADDED;
  297. event.mdevice.which = mouseID;
  298. SDL_PushEvent(&event);
  299. }
  300. }
  301. void SDL_RemoveMouse(SDL_MouseID mouseID, bool send_event)
  302. {
  303. int mouse_index = SDL_GetMouseIndex(mouseID);
  304. if (mouse_index < 0) {
  305. // We don't know about this mouse
  306. return;
  307. }
  308. SDL_free(SDL_mice[mouse_index].name);
  309. if (mouse_index != SDL_mouse_count - 1) {
  310. SDL_memmove(&SDL_mice[mouse_index], &SDL_mice[mouse_index + 1], (SDL_mouse_count - mouse_index - 1) * sizeof(SDL_mice[mouse_index]));
  311. }
  312. --SDL_mouse_count;
  313. // Remove any mouse input sources for this mouseID
  314. SDL_Mouse *mouse = SDL_GetMouse();
  315. for (int i = 0; i < mouse->num_sources; ++i) {
  316. SDL_MouseInputSource *source = &mouse->sources[i];
  317. if (source->mouseID == mouseID) {
  318. SDL_free(source->clickstate);
  319. if (i != mouse->num_sources - 1) {
  320. SDL_memmove(&mouse->sources[i], &mouse->sources[i + 1], (mouse->num_sources - i - 1) * sizeof(mouse->sources[i]));
  321. }
  322. --mouse->num_sources;
  323. break;
  324. }
  325. }
  326. if (send_event) {
  327. SDL_Event event;
  328. SDL_zero(event);
  329. event.type = SDL_EVENT_MOUSE_REMOVED;
  330. event.mdevice.which = mouseID;
  331. SDL_PushEvent(&event);
  332. }
  333. }
  334. bool SDL_HasMouse(void)
  335. {
  336. return (SDL_mouse_count > 0);
  337. }
  338. SDL_MouseID *SDL_GetMice(int *count)
  339. {
  340. int i;
  341. SDL_MouseID *mice;
  342. mice = (SDL_JoystickID *)SDL_malloc((SDL_mouse_count + 1) * sizeof(*mice));
  343. if (mice) {
  344. if (count) {
  345. *count = SDL_mouse_count;
  346. }
  347. for (i = 0; i < SDL_mouse_count; ++i) {
  348. mice[i] = SDL_mice[i].instance_id;
  349. }
  350. mice[i] = 0;
  351. } else {
  352. if (count) {
  353. *count = 0;
  354. }
  355. }
  356. return mice;
  357. }
  358. const char *SDL_GetMouseNameForID(SDL_MouseID instance_id)
  359. {
  360. int mouse_index = SDL_GetMouseIndex(instance_id);
  361. if (mouse_index < 0) {
  362. SDL_SetError("Mouse %" SDL_PRIu32 " not found", instance_id);
  363. return NULL;
  364. }
  365. return SDL_GetPersistentString(SDL_mice[mouse_index].name);
  366. }
  367. void SDL_SetDefaultCursor(SDL_Cursor *cursor)
  368. {
  369. SDL_Mouse *mouse = SDL_GetMouse();
  370. if (cursor == mouse->def_cursor) {
  371. return;
  372. }
  373. if (mouse->def_cursor) {
  374. SDL_Cursor *default_cursor = mouse->def_cursor;
  375. SDL_Cursor *prev, *curr;
  376. if (mouse->cur_cursor == mouse->def_cursor) {
  377. mouse->cur_cursor = NULL;
  378. }
  379. mouse->def_cursor = NULL;
  380. for (prev = NULL, curr = mouse->cursors; curr;
  381. prev = curr, curr = curr->next) {
  382. if (curr == default_cursor) {
  383. if (prev) {
  384. prev->next = curr->next;
  385. } else {
  386. mouse->cursors = curr->next;
  387. }
  388. break;
  389. }
  390. }
  391. if (mouse->FreeCursor && default_cursor->internal) {
  392. mouse->FreeCursor(default_cursor);
  393. } else {
  394. SDL_free(default_cursor);
  395. }
  396. }
  397. mouse->def_cursor = cursor;
  398. if (!mouse->cur_cursor) {
  399. SDL_SetCursor(cursor);
  400. }
  401. }
  402. SDL_SystemCursor SDL_GetDefaultSystemCursor(void)
  403. {
  404. SDL_SystemCursor id = SDL_SYSTEM_CURSOR_DEFAULT;
  405. const char *value = SDL_GetHint(SDL_HINT_MOUSE_DEFAULT_SYSTEM_CURSOR);
  406. if (value) {
  407. int index = SDL_atoi(value);
  408. if (0 <= index && index < SDL_SYSTEM_CURSOR_COUNT) {
  409. id = (SDL_SystemCursor)index;
  410. }
  411. }
  412. return id;
  413. }
  414. SDL_Mouse *SDL_GetMouse(void)
  415. {
  416. return &SDL_mouse;
  417. }
  418. static SDL_MouseButtonFlags SDL_GetMouseButtonState(SDL_Mouse *mouse, SDL_MouseID mouseID, bool include_touch)
  419. {
  420. int i;
  421. SDL_MouseButtonFlags buttonstate = 0;
  422. for (i = 0; i < mouse->num_sources; ++i) {
  423. if (mouseID == SDL_GLOBAL_MOUSE_ID || mouseID == SDL_TOUCH_MOUSEID) {
  424. if (include_touch || mouse->sources[i].mouseID != SDL_TOUCH_MOUSEID) {
  425. buttonstate |= mouse->sources[i].buttonstate;
  426. }
  427. } else {
  428. if (mouseID == mouse->sources[i].mouseID) {
  429. buttonstate |= mouse->sources[i].buttonstate;
  430. break;
  431. }
  432. }
  433. }
  434. return buttonstate;
  435. }
  436. SDL_Window *SDL_GetMouseFocus(void)
  437. {
  438. SDL_Mouse *mouse = SDL_GetMouse();
  439. return mouse->focus;
  440. }
  441. /* TODO RECONNECT: Hello from the Wayland video driver!
  442. * This was once removed from SDL, but it's been added back in comment form
  443. * because we will need it when Wayland adds compositor reconnect support.
  444. * If you need this before we do, great! Otherwise, leave this alone, we'll
  445. * uncomment it at the right time.
  446. * -flibit
  447. */
  448. #if 0
  449. void SDL_ResetMouse(void)
  450. {
  451. SDL_Mouse *mouse = SDL_GetMouse();
  452. Uint32 buttonState = SDL_GetMouseButtonState(mouse, SDL_GLOBAL_MOUSE_ID, false);
  453. int i;
  454. for (i = 1; i <= sizeof(buttonState)*8; ++i) {
  455. if (buttonState & SDL_BUTTON_MASK(i)) {
  456. SDL_SendMouseButton(0, mouse->focus, mouse->mouseID, i, false);
  457. }
  458. }
  459. SDL_assert(SDL_GetMouseButtonState(mouse, SDL_GLOBAL_MOUSE_ID, false) == 0);
  460. }
  461. #endif // 0
  462. void SDL_SetMouseFocus(SDL_Window *window)
  463. {
  464. SDL_Mouse *mouse = SDL_GetMouse();
  465. if (mouse->focus == window) {
  466. return;
  467. }
  468. /* Actually, this ends up being a bad idea, because most operating
  469. systems have an implicit grab when you press the mouse button down
  470. so you can drag things out of the window and then get the mouse up
  471. when it happens. So, #if 0...
  472. */
  473. #if 0
  474. if (mouse->focus && !window) {
  475. // We won't get anymore mouse messages, so reset mouse state
  476. SDL_ResetMouse();
  477. }
  478. #endif
  479. // See if the current window has lost focus
  480. if (mouse->focus) {
  481. SDL_SendWindowEvent(mouse->focus, SDL_EVENT_WINDOW_MOUSE_LEAVE, 0, 0);
  482. }
  483. mouse->focus = window;
  484. mouse->has_position = false;
  485. if (mouse->focus) {
  486. SDL_SendWindowEvent(mouse->focus, SDL_EVENT_WINDOW_MOUSE_ENTER, 0, 0);
  487. }
  488. // Update cursor visibility
  489. SDL_RedrawCursor();
  490. }
  491. bool SDL_MousePositionInWindow(SDL_Window *window, float x, float y)
  492. {
  493. if (!window) {
  494. return false;
  495. }
  496. if (window && !(window->flags & SDL_WINDOW_MOUSE_CAPTURE)) {
  497. if (x < 0.0f || y < 0.0f || x >= (float)window->w || y >= (float)window->h) {
  498. return false;
  499. }
  500. }
  501. return true;
  502. }
  503. // Check to see if we need to synthesize focus events
  504. static bool SDL_UpdateMouseFocus(SDL_Window *window, float x, float y, Uint32 buttonstate, bool send_mouse_motion)
  505. {
  506. SDL_Mouse *mouse = SDL_GetMouse();
  507. bool inWindow = SDL_MousePositionInWindow(window, x, y);
  508. if (!inWindow) {
  509. if (window == mouse->focus) {
  510. #ifdef DEBUG_MOUSE
  511. SDL_Log("Mouse left window, synthesizing move & focus lost event");
  512. #endif
  513. if (send_mouse_motion) {
  514. SDL_PrivateSendMouseMotion(0, window, SDL_GLOBAL_MOUSE_ID, false, x, y);
  515. }
  516. SDL_SetMouseFocus(NULL);
  517. }
  518. return false;
  519. }
  520. if (window != mouse->focus) {
  521. #ifdef DEBUG_MOUSE
  522. SDL_Log("Mouse entered window, synthesizing focus gain & move event");
  523. #endif
  524. SDL_SetMouseFocus(window);
  525. if (send_mouse_motion) {
  526. SDL_PrivateSendMouseMotion(0, window, SDL_GLOBAL_MOUSE_ID, false, x, y);
  527. }
  528. }
  529. return true;
  530. }
  531. void SDL_SendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, bool relative, float x, float y)
  532. {
  533. if (window && !relative) {
  534. SDL_Mouse *mouse = SDL_GetMouse();
  535. if (!SDL_UpdateMouseFocus(window, x, y, SDL_GetMouseButtonState(mouse, mouseID, true), (mouseID != SDL_TOUCH_MOUSEID && mouseID != SDL_PEN_MOUSEID))) {
  536. return;
  537. }
  538. }
  539. SDL_PrivateSendMouseMotion(timestamp, window, mouseID, relative, x, y);
  540. }
  541. static void ConstrainMousePosition(SDL_Mouse *mouse, SDL_Window *window, float *x, float *y)
  542. {
  543. /* make sure that the pointers find themselves inside the windows,
  544. unless we have the mouse captured. */
  545. if (window && !(window->flags & SDL_WINDOW_MOUSE_CAPTURE)) {
  546. int x_min = 0, x_max = window->w - 1;
  547. int y_min = 0, y_max = window->h - 1;
  548. const SDL_Rect *confine = SDL_GetWindowMouseRect(window);
  549. if (confine) {
  550. SDL_Rect window_rect;
  551. SDL_Rect mouse_rect;
  552. window_rect.x = 0;
  553. window_rect.y = 0;
  554. window_rect.w = x_max + 1;
  555. window_rect.h = y_max + 1;
  556. if (SDL_GetRectIntersection(confine, &window_rect, &mouse_rect)) {
  557. x_min = mouse_rect.x;
  558. y_min = mouse_rect.y;
  559. x_max = x_min + mouse_rect.w - 1;
  560. y_max = y_min + mouse_rect.h - 1;
  561. }
  562. }
  563. if (*x >= (float)(x_max + 1)) {
  564. *x = SDL_max((float)x_max, mouse->last_x);
  565. }
  566. if (*x < (float)x_min) {
  567. *x = (float)x_min;
  568. }
  569. if (*y >= (float)(y_max + 1)) {
  570. *y = SDL_max((float)y_max, mouse->last_y);
  571. }
  572. if (*y < (float)y_min) {
  573. *y = (float)y_min;
  574. }
  575. }
  576. }
  577. static void SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, bool relative, float x, float y)
  578. {
  579. SDL_Mouse *mouse = SDL_GetMouse();
  580. float xrel = 0.0f;
  581. float yrel = 0.0f;
  582. bool window_is_relative = mouse->focus && (mouse->focus->flags & SDL_WINDOW_MOUSE_RELATIVE_MODE);
  583. // SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events
  584. if (mouse->mouse_touch_events) {
  585. if (mouseID != SDL_TOUCH_MOUSEID && mouseID != SDL_PEN_MOUSEID && !relative && track_mouse_down) {
  586. if (window) {
  587. float normalized_x = x / (float)window->w;
  588. float normalized_y = y / (float)window->h;
  589. SDL_SendTouchMotion(timestamp, SDL_MOUSE_TOUCHID, SDL_BUTTON_LEFT, window, normalized_x, normalized_y, 1.0f);
  590. }
  591. }
  592. }
  593. // SDL_HINT_TOUCH_MOUSE_EVENTS: if not set, discard synthetic mouse events coming from platform layer
  594. if (!mouse->touch_mouse_events && mouseID == SDL_TOUCH_MOUSEID) {
  595. return;
  596. }
  597. if (relative) {
  598. if (mouse->relative_mode) {
  599. if (mouse->InputTransform) {
  600. void *data = mouse->input_transform_data;
  601. mouse->InputTransform(data, timestamp, window, mouseID, &x, &y);
  602. } else {
  603. if (mouse->enable_relative_system_scale) {
  604. if (mouse->ApplySystemScale) {
  605. mouse->ApplySystemScale(mouse->system_scale_data, timestamp, window, mouseID, &x, &y);
  606. }
  607. }
  608. if (mouse->enable_relative_speed_scale) {
  609. x *= mouse->relative_speed_scale;
  610. y *= mouse->relative_speed_scale;
  611. }
  612. }
  613. } else {
  614. if (mouse->enable_normal_speed_scale) {
  615. x *= mouse->normal_speed_scale;
  616. y *= mouse->normal_speed_scale;
  617. }
  618. }
  619. if (mouse->integer_mode_flags & 1) {
  620. // Accumulate the fractional relative motion and only process the integer portion
  621. mouse->integer_mode_residual_motion_x = SDL_modff(mouse->integer_mode_residual_motion_x + x, &x);
  622. mouse->integer_mode_residual_motion_y = SDL_modff(mouse->integer_mode_residual_motion_y + y, &y);
  623. }
  624. xrel = x;
  625. yrel = y;
  626. x = (mouse->last_x + xrel);
  627. y = (mouse->last_y + yrel);
  628. ConstrainMousePosition(mouse, window, &x, &y);
  629. } else {
  630. if (mouse->integer_mode_flags & 1) {
  631. // Discard the fractional component from absolute coordinates
  632. x = SDL_truncf(x);
  633. y = SDL_truncf(y);
  634. }
  635. ConstrainMousePosition(mouse, window, &x, &y);
  636. if (mouse->has_position) {
  637. xrel = x - mouse->last_x;
  638. yrel = y - mouse->last_y;
  639. }
  640. }
  641. if (mouse->has_position && xrel == 0.0f && yrel == 0.0f) { // Drop events that don't change state
  642. #ifdef DEBUG_MOUSE
  643. SDL_Log("Mouse event didn't change state - dropped!");
  644. #endif
  645. return;
  646. }
  647. // Ignore relative motion positioning the first touch
  648. if (mouseID == SDL_TOUCH_MOUSEID && !SDL_GetMouseButtonState(mouse, mouseID, true)) {
  649. xrel = 0.0f;
  650. yrel = 0.0f;
  651. }
  652. // modify internal state
  653. {
  654. mouse->x_accu += xrel;
  655. mouse->y_accu += yrel;
  656. if (relative && mouse->has_position) {
  657. mouse->x += xrel;
  658. mouse->y += yrel;
  659. ConstrainMousePosition(mouse, window, &mouse->x, &mouse->y);
  660. } else {
  661. mouse->x = x;
  662. mouse->y = y;
  663. }
  664. mouse->has_position = true;
  665. // Use unclamped values if we're getting events outside the window
  666. mouse->last_x = relative ? mouse->x : x;
  667. mouse->last_y = relative ? mouse->y : y;
  668. mouse->click_motion_x += xrel;
  669. mouse->click_motion_y += yrel;
  670. }
  671. // Move the mouse cursor, if needed
  672. if (mouse->cursor_visible && !mouse->relative_mode &&
  673. mouse->MoveCursor && mouse->cur_cursor) {
  674. mouse->MoveCursor(mouse->cur_cursor);
  675. }
  676. // Post the event, if desired
  677. if (SDL_EventEnabled(SDL_EVENT_MOUSE_MOTION)) {
  678. if ((!mouse->relative_mode || mouse->warp_emulation_active) && mouseID != SDL_TOUCH_MOUSEID && mouseID != SDL_PEN_MOUSEID) {
  679. // We're not in relative mode, so all mouse events are global mouse events
  680. mouseID = SDL_GLOBAL_MOUSE_ID;
  681. }
  682. if (!relative && window_is_relative) {
  683. if (!mouse->relative_mode_warp_motion) {
  684. return;
  685. }
  686. xrel = 0.0f;
  687. yrel = 0.0f;
  688. }
  689. SDL_Event event;
  690. event.type = SDL_EVENT_MOUSE_MOTION;
  691. event.common.timestamp = timestamp;
  692. event.motion.windowID = mouse->focus ? mouse->focus->id : 0;
  693. event.motion.which = mouseID;
  694. // Set us pending (or clear during a normal mouse movement event) as having triggered
  695. mouse->was_touch_mouse_events = (mouseID == SDL_TOUCH_MOUSEID);
  696. event.motion.state = SDL_GetMouseButtonState(mouse, mouseID, true);
  697. event.motion.x = mouse->x;
  698. event.motion.y = mouse->y;
  699. event.motion.xrel = xrel;
  700. event.motion.yrel = yrel;
  701. SDL_PushEvent(&event);
  702. }
  703. }
  704. static SDL_MouseInputSource *GetMouseInputSource(SDL_Mouse *mouse, SDL_MouseID mouseID, bool down, Uint8 button)
  705. {
  706. SDL_MouseInputSource *source, *match = NULL, *sources;
  707. int i;
  708. for (i = 0; i < mouse->num_sources; ++i) {
  709. source = &mouse->sources[i];
  710. if (source->mouseID == mouseID) {
  711. match = source;
  712. break;
  713. }
  714. }
  715. if (!down && (!match || !(match->buttonstate & SDL_BUTTON_MASK(button)))) {
  716. /* This might be a button release from a transition between mouse messages and raw input.
  717. * See if there's another mouse source that already has that button down and use that.
  718. */
  719. for (i = 0; i < mouse->num_sources; ++i) {
  720. source = &mouse->sources[i];
  721. if ((source->buttonstate & SDL_BUTTON_MASK(button))) {
  722. match = source;
  723. break;
  724. }
  725. }
  726. }
  727. if (match) {
  728. return match;
  729. }
  730. sources = (SDL_MouseInputSource *)SDL_realloc(mouse->sources, (mouse->num_sources + 1) * sizeof(*mouse->sources));
  731. if (sources) {
  732. mouse->sources = sources;
  733. ++mouse->num_sources;
  734. source = &sources[mouse->num_sources - 1];
  735. SDL_zerop(source);
  736. source->mouseID = mouseID;
  737. return source;
  738. }
  739. return NULL;
  740. }
  741. static SDL_MouseClickState *GetMouseClickState(SDL_MouseInputSource *source, Uint8 button)
  742. {
  743. if (button >= source->num_clickstates) {
  744. int i, count = button + 1;
  745. SDL_MouseClickState *clickstate = (SDL_MouseClickState *)SDL_realloc(source->clickstate, count * sizeof(*source->clickstate));
  746. if (!clickstate) {
  747. return NULL;
  748. }
  749. source->clickstate = clickstate;
  750. for (i = source->num_clickstates; i < count; ++i) {
  751. SDL_zero(source->clickstate[i]);
  752. }
  753. source->num_clickstates = count;
  754. }
  755. return &source->clickstate[button];
  756. }
  757. static void SDL_PrivateSendMouseButton(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, Uint8 button, bool down, int clicks)
  758. {
  759. SDL_Mouse *mouse = SDL_GetMouse();
  760. SDL_EventType type;
  761. Uint32 buttonstate;
  762. SDL_MouseInputSource *source;
  763. source = GetMouseInputSource(mouse, mouseID, down, button);
  764. if (!source) {
  765. return;
  766. }
  767. buttonstate = source->buttonstate;
  768. // SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events
  769. if (mouse->mouse_touch_events) {
  770. if (mouseID != SDL_TOUCH_MOUSEID && mouseID != SDL_PEN_MOUSEID && button == SDL_BUTTON_LEFT) {
  771. if (down) {
  772. track_mouse_down = true;
  773. } else {
  774. track_mouse_down = false;
  775. }
  776. if (window) {
  777. type = track_mouse_down ? SDL_EVENT_FINGER_DOWN : SDL_EVENT_FINGER_UP;
  778. float normalized_x = mouse->x / (float)window->w;
  779. float normalized_y = mouse->y / (float)window->h;
  780. SDL_SendTouch(timestamp, SDL_MOUSE_TOUCHID, SDL_BUTTON_LEFT, window, type, normalized_x, normalized_y, 1.0f);
  781. }
  782. }
  783. }
  784. // SDL_HINT_TOUCH_MOUSE_EVENTS: if not set, discard synthetic mouse events coming from platform layer
  785. if (mouse->touch_mouse_events == 0) {
  786. if (mouseID == SDL_TOUCH_MOUSEID) {
  787. return;
  788. }
  789. }
  790. // Figure out which event to perform
  791. if (down) {
  792. type = SDL_EVENT_MOUSE_BUTTON_DOWN;
  793. buttonstate |= SDL_BUTTON_MASK(button);
  794. } else {
  795. type = SDL_EVENT_MOUSE_BUTTON_UP;
  796. buttonstate &= ~SDL_BUTTON_MASK(button);
  797. }
  798. // We do this after calculating buttonstate so button presses gain focus
  799. if (window && down) {
  800. SDL_UpdateMouseFocus(window, mouse->x, mouse->y, buttonstate, true);
  801. }
  802. if (buttonstate == source->buttonstate) {
  803. // Ignore this event, no state change
  804. return;
  805. }
  806. source->buttonstate = buttonstate;
  807. if (clicks < 0) {
  808. SDL_MouseClickState *clickstate = GetMouseClickState(source, button);
  809. if (clickstate) {
  810. if (down) {
  811. Uint64 now = SDL_GetTicks();
  812. if (now >= (clickstate->last_timestamp + mouse->double_click_time) ||
  813. SDL_fabs(mouse->click_motion_x - clickstate->click_motion_x) > mouse->double_click_radius ||
  814. SDL_fabs(mouse->click_motion_y - clickstate->click_motion_y) > mouse->double_click_radius) {
  815. clickstate->click_count = 0;
  816. }
  817. clickstate->last_timestamp = now;
  818. clickstate->click_motion_x = mouse->click_motion_x;
  819. clickstate->click_motion_y = mouse->click_motion_y;
  820. if (clickstate->click_count < 255) {
  821. ++clickstate->click_count;
  822. }
  823. }
  824. clicks = clickstate->click_count;
  825. } else {
  826. clicks = 1;
  827. }
  828. }
  829. // Post the event, if desired
  830. if (SDL_EventEnabled(type)) {
  831. if ((!mouse->relative_mode || mouse->warp_emulation_active) && mouseID != SDL_TOUCH_MOUSEID && mouseID != SDL_PEN_MOUSEID) {
  832. // We're not in relative mode, so all mouse events are global mouse events
  833. mouseID = SDL_GLOBAL_MOUSE_ID;
  834. } else {
  835. mouseID = source->mouseID;
  836. }
  837. SDL_Event event;
  838. event.type = type;
  839. event.common.timestamp = timestamp;
  840. event.button.windowID = mouse->focus ? mouse->focus->id : 0;
  841. event.button.which = mouseID;
  842. event.button.down = down;
  843. event.button.button = button;
  844. event.button.clicks = (Uint8)SDL_min(clicks, 255);
  845. event.button.x = mouse->x;
  846. event.button.y = mouse->y;
  847. SDL_PushEvent(&event);
  848. }
  849. // We do this after dispatching event so button releases can lose focus
  850. if (window && !down) {
  851. SDL_UpdateMouseFocus(window, mouse->x, mouse->y, buttonstate, true);
  852. }
  853. // Automatically capture the mouse while buttons are pressed
  854. if (mouse->auto_capture) {
  855. SDL_UpdateMouseCapture(false);
  856. }
  857. }
  858. void SDL_SendMouseButtonClicks(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, Uint8 button, bool down, int clicks)
  859. {
  860. clicks = SDL_max(clicks, 0);
  861. SDL_PrivateSendMouseButton(timestamp, window, mouseID, button, down, clicks);
  862. }
  863. void SDL_SendMouseButton(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, Uint8 button, bool down)
  864. {
  865. SDL_PrivateSendMouseButton(timestamp, window, mouseID, button, down, -1);
  866. }
  867. void SDL_SendMouseWheel(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, float x, float y, SDL_MouseWheelDirection direction)
  868. {
  869. SDL_Mouse *mouse = SDL_GetMouse();
  870. if (window) {
  871. SDL_SetMouseFocus(window);
  872. }
  873. if (x == 0.0f && y == 0.0f) {
  874. return;
  875. }
  876. // Post the event, if desired
  877. if (SDL_EventEnabled(SDL_EVENT_MOUSE_WHEEL)) {
  878. float integer_x, integer_y;
  879. if (!mouse->relative_mode || mouse->warp_emulation_active) {
  880. // We're not in relative mode, so all mouse events are global mouse events
  881. mouseID = SDL_GLOBAL_MOUSE_ID;
  882. }
  883. SDL_Event event;
  884. event.type = SDL_EVENT_MOUSE_WHEEL;
  885. event.common.timestamp = timestamp;
  886. event.wheel.windowID = mouse->focus ? mouse->focus->id : 0;
  887. event.wheel.which = mouseID;
  888. event.wheel.direction = direction;
  889. event.wheel.mouse_x = mouse->x;
  890. event.wheel.mouse_y = mouse->y;
  891. mouse->residual_scroll_x = SDL_modff(mouse->residual_scroll_x + x, &integer_x);
  892. event.wheel.integer_x = (Sint32)integer_x;
  893. mouse->residual_scroll_y = SDL_modff(mouse->residual_scroll_y + y, &integer_y);
  894. event.wheel.integer_y = (Sint32)integer_y;
  895. // Return the accumulated values in x/y when integer wheel mode is enabled.
  896. // This is necessary for compatibility with sdl2-compat 2.32.54.
  897. if (mouse->integer_mode_flags & 2) {
  898. event.wheel.x = integer_x;
  899. event.wheel.y = integer_y;
  900. } else {
  901. event.wheel.x = x;
  902. event.wheel.y = y;
  903. }
  904. SDL_PushEvent(&event);
  905. }
  906. }
  907. void SDL_QuitMouse(void)
  908. {
  909. SDL_Cursor *cursor, *next;
  910. SDL_Mouse *mouse = SDL_GetMouse();
  911. if (mouse->added_mouse_touch_device) {
  912. SDL_DelTouch(SDL_MOUSE_TOUCHID);
  913. mouse->added_mouse_touch_device = false;
  914. }
  915. if (mouse->added_pen_touch_device) {
  916. SDL_DelTouch(SDL_PEN_TOUCHID);
  917. mouse->added_pen_touch_device = false;
  918. }
  919. if (mouse->CaptureMouse) {
  920. SDL_CaptureMouse(false);
  921. SDL_UpdateMouseCapture(true);
  922. }
  923. SDL_SetRelativeMouseMode(false);
  924. SDL_ShowCursor();
  925. if (mouse->def_cursor) {
  926. SDL_SetDefaultCursor(NULL);
  927. }
  928. cursor = mouse->cursors;
  929. while (cursor) {
  930. next = cursor->next;
  931. SDL_DestroyCursor(cursor);
  932. cursor = next;
  933. }
  934. mouse->cursors = NULL;
  935. mouse->cur_cursor = NULL;
  936. if (mouse->sources) {
  937. for (int i = 0; i < mouse->num_sources; ++i) {
  938. SDL_MouseInputSource *source = &mouse->sources[i];
  939. SDL_free(source->clickstate);
  940. }
  941. SDL_free(mouse->sources);
  942. mouse->sources = NULL;
  943. }
  944. mouse->num_sources = 0;
  945. SDL_RemoveHintCallback(SDL_HINT_MOUSE_DOUBLE_CLICK_TIME,
  946. SDL_MouseDoubleClickTimeChanged, mouse);
  947. SDL_RemoveHintCallback(SDL_HINT_MOUSE_DOUBLE_CLICK_RADIUS,
  948. SDL_MouseDoubleClickRadiusChanged, mouse);
  949. SDL_RemoveHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
  950. SDL_MouseNormalSpeedScaleChanged, mouse);
  951. SDL_RemoveHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
  952. SDL_MouseRelativeSpeedScaleChanged, mouse);
  953. SDL_RemoveHintCallback(SDL_HINT_MOUSE_RELATIVE_SYSTEM_SCALE,
  954. SDL_MouseRelativeSystemScaleChanged, mouse);
  955. SDL_RemoveHintCallback(SDL_HINT_MOUSE_RELATIVE_MODE_CENTER,
  956. SDL_MouseRelativeModeCenterChanged, mouse);
  957. SDL_RemoveHintCallback(SDL_HINT_MOUSE_EMULATE_WARP_WITH_RELATIVE,
  958. SDL_MouseWarpEmulationChanged, mouse);
  959. SDL_RemoveHintCallback(SDL_HINT_TOUCH_MOUSE_EVENTS,
  960. SDL_TouchMouseEventsChanged, mouse);
  961. SDL_RemoveHintCallback(SDL_HINT_MOUSE_TOUCH_EVENTS,
  962. SDL_MouseTouchEventsChanged, mouse);
  963. SDL_RemoveHintCallback(SDL_HINT_PEN_MOUSE_EVENTS,
  964. SDL_PenMouseEventsChanged, mouse);
  965. SDL_RemoveHintCallback(SDL_HINT_PEN_TOUCH_EVENTS,
  966. SDL_PenTouchEventsChanged, mouse);
  967. SDL_RemoveHintCallback(SDL_HINT_MOUSE_AUTO_CAPTURE,
  968. SDL_MouseAutoCaptureChanged, mouse);
  969. SDL_RemoveHintCallback(SDL_HINT_MOUSE_RELATIVE_WARP_MOTION,
  970. SDL_MouseRelativeWarpMotionChanged, mouse);
  971. SDL_RemoveHintCallback(SDL_HINT_MOUSE_RELATIVE_CURSOR_VISIBLE,
  972. SDL_MouseRelativeCursorVisibleChanged, mouse);
  973. SDL_RemoveHintCallback("SDL_MOUSE_INTEGER_MODE",
  974. SDL_MouseIntegerModeChanged, mouse);
  975. for (int i = SDL_mouse_count; i--; ) {
  976. SDL_RemoveMouse(SDL_mice[i].instance_id, false);
  977. }
  978. SDL_free(SDL_mice);
  979. SDL_mice = NULL;
  980. if (mouse->internal) {
  981. SDL_free(mouse->internal);
  982. mouse->internal = NULL;
  983. }
  984. SDL_zerop(mouse);
  985. }
  986. bool SDL_SetRelativeMouseTransform(SDL_MouseMotionTransformCallback transform, void *userdata)
  987. {
  988. SDL_Mouse *mouse = SDL_GetMouse();
  989. if (mouse->relative_mode) {
  990. return SDL_SetError("Can't set mouse transform while relative mode is active");
  991. }
  992. mouse->InputTransform = transform;
  993. mouse->input_transform_data = userdata;
  994. return true;
  995. }
  996. SDL_MouseButtonFlags SDL_GetMouseState(float *x, float *y)
  997. {
  998. SDL_Mouse *mouse = SDL_GetMouse();
  999. if (x) {
  1000. *x = mouse->x;
  1001. }
  1002. if (y) {
  1003. *y = mouse->y;
  1004. }
  1005. return SDL_GetMouseButtonState(mouse, SDL_GLOBAL_MOUSE_ID, true);
  1006. }
  1007. SDL_MouseButtonFlags SDL_GetRelativeMouseState(float *x, float *y)
  1008. {
  1009. SDL_Mouse *mouse = SDL_GetMouse();
  1010. if (x) {
  1011. *x = mouse->x_accu;
  1012. }
  1013. if (y) {
  1014. *y = mouse->y_accu;
  1015. }
  1016. mouse->x_accu = 0.0f;
  1017. mouse->y_accu = 0.0f;
  1018. return SDL_GetMouseButtonState(mouse, SDL_GLOBAL_MOUSE_ID, true);
  1019. }
  1020. SDL_MouseButtonFlags SDL_GetGlobalMouseState(float *x, float *y)
  1021. {
  1022. SDL_Mouse *mouse = SDL_GetMouse();
  1023. if (mouse->GetGlobalMouseState) {
  1024. float tmpx, tmpy;
  1025. // make sure these are never NULL for the backend implementations...
  1026. if (!x) {
  1027. x = &tmpx;
  1028. }
  1029. if (!y) {
  1030. y = &tmpy;
  1031. }
  1032. *x = *y = 0.0f;
  1033. return mouse->GetGlobalMouseState(x, y);
  1034. } else {
  1035. return SDL_GetMouseState(x, y);
  1036. }
  1037. }
  1038. void SDL_PerformWarpMouseInWindow(SDL_Window *window, float x, float y, bool ignore_relative_mode)
  1039. {
  1040. SDL_Mouse *mouse = SDL_GetMouse();
  1041. if (!window) {
  1042. window = mouse->focus;
  1043. }
  1044. if (!window) {
  1045. return;
  1046. }
  1047. if ((window->flags & SDL_WINDOW_MINIMIZED) == SDL_WINDOW_MINIMIZED) {
  1048. return;
  1049. }
  1050. // Ignore the previous position when we warp
  1051. mouse->last_x = x;
  1052. mouse->last_y = y;
  1053. mouse->has_position = false;
  1054. if (mouse->relative_mode && !ignore_relative_mode) {
  1055. /* 2.0.22 made warping in relative mode actually functional, which
  1056. * surprised many applications that weren't expecting the additional
  1057. * mouse motion.
  1058. *
  1059. * So for now, warping in relative mode adjusts the absolution position
  1060. * but doesn't generate motion events, unless SDL_HINT_MOUSE_RELATIVE_WARP_MOTION is set.
  1061. */
  1062. if (!mouse->relative_mode_warp_motion) {
  1063. mouse->x = x;
  1064. mouse->y = y;
  1065. mouse->has_position = true;
  1066. return;
  1067. }
  1068. }
  1069. if (mouse->WarpMouse && !mouse->relative_mode) {
  1070. mouse->WarpMouse(window, x, y);
  1071. } else {
  1072. SDL_PrivateSendMouseMotion(0, window, SDL_GLOBAL_MOUSE_ID, false, x, y);
  1073. }
  1074. }
  1075. void SDL_DisableMouseWarpEmulation(void)
  1076. {
  1077. SDL_Mouse *mouse = SDL_GetMouse();
  1078. if (mouse->warp_emulation_active) {
  1079. SDL_SetRelativeMouseMode(false);
  1080. }
  1081. mouse->warp_emulation_prohibited = true;
  1082. }
  1083. static void SDL_MaybeEnableWarpEmulation(SDL_Window *window, float x, float y)
  1084. {
  1085. SDL_Mouse *mouse = SDL_GetMouse();
  1086. if (!mouse->warp_emulation_prohibited && mouse->warp_emulation_hint && !mouse->cursor_visible && !mouse->warp_emulation_active) {
  1087. if (!window) {
  1088. window = mouse->focus;
  1089. }
  1090. if (window) {
  1091. const float cx = window->w / 2.f;
  1092. const float cy = window->h / 2.f;
  1093. if (x >= SDL_floorf(cx) && x <= SDL_ceilf(cx) &&
  1094. y >= SDL_floorf(cy) && y <= SDL_ceilf(cy)) {
  1095. // Require two consecutive warps to the center within a certain timespan to enter warp emulation mode.
  1096. const Uint64 now = SDL_GetTicksNS();
  1097. if (now - mouse->last_center_warp_time_ns < WARP_EMULATION_THRESHOLD_NS) {
  1098. mouse->warp_emulation_active = true;
  1099. if (!SDL_SetRelativeMouseMode(true)) {
  1100. mouse->warp_emulation_active = false;
  1101. }
  1102. }
  1103. mouse->last_center_warp_time_ns = now;
  1104. return;
  1105. }
  1106. }
  1107. mouse->last_center_warp_time_ns = 0;
  1108. }
  1109. }
  1110. void SDL_WarpMouseInWindow(SDL_Window *window, float x, float y)
  1111. {
  1112. SDL_Mouse *mouse = SDL_GetMouse();
  1113. SDL_MaybeEnableWarpEmulation(window, x, y);
  1114. SDL_PerformWarpMouseInWindow(window, x, y, mouse->warp_emulation_active);
  1115. }
  1116. bool SDL_WarpMouseGlobal(float x, float y)
  1117. {
  1118. SDL_Mouse *mouse = SDL_GetMouse();
  1119. if (mouse->WarpMouseGlobal) {
  1120. return mouse->WarpMouseGlobal(x, y);
  1121. }
  1122. return SDL_Unsupported();
  1123. }
  1124. bool SDL_SetRelativeMouseMode(bool enabled)
  1125. {
  1126. SDL_Mouse *mouse = SDL_GetMouse();
  1127. SDL_Window *focusWindow = SDL_GetKeyboardFocus();
  1128. if (!enabled) {
  1129. // If warps were being emulated, reset the flag.
  1130. mouse->warp_emulation_active = false;
  1131. }
  1132. if (enabled == mouse->relative_mode) {
  1133. return true;
  1134. }
  1135. // Set the relative mode
  1136. if (!mouse->SetRelativeMouseMode || !mouse->SetRelativeMouseMode(enabled)) {
  1137. if (enabled) {
  1138. return SDL_SetError("No relative mode implementation available");
  1139. }
  1140. }
  1141. mouse->relative_mode = enabled;
  1142. if (enabled) {
  1143. // Update cursor visibility before we potentially warp the mouse
  1144. SDL_RedrawCursor();
  1145. }
  1146. if (enabled && focusWindow) {
  1147. SDL_SetMouseFocus(focusWindow);
  1148. }
  1149. if (focusWindow) {
  1150. SDL_UpdateWindowGrab(focusWindow);
  1151. // Put the cursor back to where the application expects it
  1152. if (!enabled) {
  1153. SDL_PerformWarpMouseInWindow(focusWindow, mouse->x, mouse->y, true);
  1154. }
  1155. SDL_UpdateMouseCapture(false);
  1156. }
  1157. if (!enabled) {
  1158. // Update cursor visibility after we restore the mouse position
  1159. SDL_RedrawCursor();
  1160. }
  1161. // Flush pending mouse motion - ideally we would pump events, but that's not always safe
  1162. SDL_FlushEvent(SDL_EVENT_MOUSE_MOTION);
  1163. return true;
  1164. }
  1165. bool SDL_GetRelativeMouseMode(void)
  1166. {
  1167. SDL_Mouse *mouse = SDL_GetMouse();
  1168. return mouse->relative_mode;
  1169. }
  1170. void SDL_UpdateRelativeMouseMode(void)
  1171. {
  1172. SDL_Mouse *mouse = SDL_GetMouse();
  1173. SDL_Window *focus = SDL_GetKeyboardFocus();
  1174. bool relative_mode = (focus && (focus->flags & SDL_WINDOW_MOUSE_RELATIVE_MODE));
  1175. if (relative_mode != mouse->relative_mode) {
  1176. SDL_SetRelativeMouseMode(relative_mode);
  1177. }
  1178. }
  1179. bool SDL_UpdateMouseCapture(bool force_release)
  1180. {
  1181. SDL_Mouse *mouse = SDL_GetMouse();
  1182. SDL_Window *capture_window = NULL;
  1183. if (!mouse->CaptureMouse) {
  1184. return true;
  1185. }
  1186. if (!force_release) {
  1187. if (SDL_GetMessageBoxCount() == 0 &&
  1188. (mouse->capture_desired || (mouse->auto_capture && SDL_GetMouseButtonState(mouse, SDL_GLOBAL_MOUSE_ID, false) != 0))) {
  1189. if (!mouse->relative_mode) {
  1190. capture_window = mouse->focus;
  1191. }
  1192. }
  1193. }
  1194. if (capture_window != mouse->capture_window) {
  1195. /* We can get here recursively on Windows, so make sure we complete
  1196. * all of the window state operations before we change the capture state
  1197. * (e.g. https://github.com/libsdl-org/SDL/pull/5608)
  1198. */
  1199. SDL_Window *previous_capture = mouse->capture_window;
  1200. if (previous_capture) {
  1201. previous_capture->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
  1202. }
  1203. if (capture_window) {
  1204. capture_window->flags |= SDL_WINDOW_MOUSE_CAPTURE;
  1205. }
  1206. mouse->capture_window = capture_window;
  1207. if (!mouse->CaptureMouse(capture_window)) {
  1208. // CaptureMouse() will have set an error, just restore the state
  1209. if (previous_capture) {
  1210. previous_capture->flags |= SDL_WINDOW_MOUSE_CAPTURE;
  1211. }
  1212. if (capture_window) {
  1213. capture_window->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
  1214. }
  1215. mouse->capture_window = previous_capture;
  1216. return false;
  1217. }
  1218. }
  1219. return true;
  1220. }
  1221. bool SDL_CaptureMouse(bool enabled)
  1222. {
  1223. SDL_Mouse *mouse = SDL_GetMouse();
  1224. if (!mouse->CaptureMouse) {
  1225. return SDL_Unsupported();
  1226. }
  1227. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  1228. /* Windows mouse capture is tied to the current thread, and must be called
  1229. * from the thread that created the window being captured. Since we update
  1230. * the mouse capture state from the event processing, any application state
  1231. * changes must be processed on that thread as well.
  1232. */
  1233. if (!SDL_OnVideoThread()) {
  1234. return SDL_SetError("SDL_CaptureMouse() must be called on the main thread");
  1235. }
  1236. #endif // defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  1237. if (enabled && SDL_GetKeyboardFocus() == NULL) {
  1238. return SDL_SetError("No window has focus");
  1239. }
  1240. mouse->capture_desired = enabled;
  1241. return SDL_UpdateMouseCapture(false);
  1242. }
  1243. SDL_Cursor *SDL_CreateCursor(const Uint8 *data, const Uint8 *mask, int w, int h, int hot_x, int hot_y)
  1244. {
  1245. SDL_Surface *surface;
  1246. SDL_Cursor *cursor;
  1247. int x, y;
  1248. Uint32 *pixels;
  1249. Uint8 datab = 0, maskb = 0;
  1250. const Uint32 black = 0xFF000000;
  1251. const Uint32 white = 0xFFFFFFFF;
  1252. const Uint32 transparent = 0x00000000;
  1253. #if defined(SDL_PLATFORM_WIN32)
  1254. // Only Windows backend supports inverted pixels in mono cursors.
  1255. const Uint32 inverted = 0x00FFFFFF;
  1256. #else
  1257. const Uint32 inverted = 0xFF000000;
  1258. #endif // defined(SDL_PLATFORM_WIN32)
  1259. // Make sure the width is a multiple of 8
  1260. w = ((w + 7) & ~7);
  1261. // Create the surface from a bitmap
  1262. surface = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_ARGB8888);
  1263. if (!surface) {
  1264. return NULL;
  1265. }
  1266. for (y = 0; y < h; ++y) {
  1267. pixels = (Uint32 *)((Uint8 *)surface->pixels + y * surface->pitch);
  1268. for (x = 0; x < w; ++x) {
  1269. if ((x % 8) == 0) {
  1270. datab = *data++;
  1271. maskb = *mask++;
  1272. }
  1273. if (maskb & 0x80) {
  1274. *pixels++ = (datab & 0x80) ? black : white;
  1275. } else {
  1276. *pixels++ = (datab & 0x80) ? inverted : transparent;
  1277. }
  1278. datab <<= 1;
  1279. maskb <<= 1;
  1280. }
  1281. }
  1282. cursor = SDL_CreateColorCursor(surface, hot_x, hot_y);
  1283. SDL_DestroySurface(surface);
  1284. return cursor;
  1285. }
  1286. SDL_Cursor *SDL_CreateColorCursor(SDL_Surface *surface, int hot_x, int hot_y)
  1287. {
  1288. SDL_Mouse *mouse = SDL_GetMouse();
  1289. SDL_Surface *temp = NULL;
  1290. SDL_Cursor *cursor;
  1291. if (!surface) {
  1292. SDL_InvalidParamError("surface");
  1293. return NULL;
  1294. }
  1295. // Allow specifying the hot spot via properties on the surface
  1296. SDL_PropertiesID props = SDL_GetSurfaceProperties(surface);
  1297. hot_x = (int)SDL_GetNumberProperty(props, SDL_PROP_SURFACE_HOTSPOT_X_NUMBER, hot_x);
  1298. hot_y = (int)SDL_GetNumberProperty(props, SDL_PROP_SURFACE_HOTSPOT_Y_NUMBER, hot_y);
  1299. // Sanity check the hot spot
  1300. if ((hot_x < 0) || (hot_y < 0) ||
  1301. (hot_x >= surface->w) || (hot_y >= surface->h)) {
  1302. SDL_SetError("Cursor hot spot doesn't lie within cursor");
  1303. return NULL;
  1304. }
  1305. if (surface->format != SDL_PIXELFORMAT_ARGB8888) {
  1306. temp = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_ARGB8888);
  1307. if (!temp) {
  1308. return NULL;
  1309. }
  1310. surface = temp;
  1311. }
  1312. if (mouse->CreateCursor) {
  1313. cursor = mouse->CreateCursor(surface, hot_x, hot_y);
  1314. } else {
  1315. cursor = (SDL_Cursor *)SDL_calloc(1, sizeof(*cursor));
  1316. }
  1317. if (cursor) {
  1318. cursor->next = mouse->cursors;
  1319. mouse->cursors = cursor;
  1320. }
  1321. SDL_DestroySurface(temp);
  1322. return cursor;
  1323. }
  1324. SDL_Cursor *SDL_CreateSystemCursor(SDL_SystemCursor id)
  1325. {
  1326. SDL_Mouse *mouse = SDL_GetMouse();
  1327. SDL_Cursor *cursor;
  1328. if (!mouse->CreateSystemCursor) {
  1329. SDL_SetError("CreateSystemCursor is not currently supported");
  1330. return NULL;
  1331. }
  1332. cursor = mouse->CreateSystemCursor(id);
  1333. if (cursor) {
  1334. cursor->next = mouse->cursors;
  1335. mouse->cursors = cursor;
  1336. }
  1337. return cursor;
  1338. }
  1339. void SDL_RedrawCursor(void)
  1340. {
  1341. SDL_Mouse *mouse = SDL_GetMouse();
  1342. SDL_Cursor *cursor;
  1343. if (mouse->focus) {
  1344. cursor = mouse->cur_cursor;
  1345. } else {
  1346. cursor = mouse->def_cursor;
  1347. }
  1348. if (mouse->focus && (!mouse->cursor_visible || (mouse->relative_mode && mouse->relative_mode_hide_cursor))) {
  1349. cursor = NULL;
  1350. }
  1351. if (mouse->ShowCursor) {
  1352. mouse->ShowCursor(cursor);
  1353. }
  1354. }
  1355. /* SDL_SetCursor(NULL) can be used to force the cursor redraw,
  1356. if this is desired for any reason. This is used when setting
  1357. the video mode and when the SDL window gains the mouse focus.
  1358. */
  1359. bool SDL_SetCursor(SDL_Cursor *cursor)
  1360. {
  1361. SDL_Mouse *mouse = SDL_GetMouse();
  1362. // already on this cursor, no further action required
  1363. if (cursor == mouse->cur_cursor) {
  1364. return true;
  1365. }
  1366. // Set the new cursor
  1367. if (cursor) {
  1368. // Make sure the cursor is still valid for this mouse
  1369. if (cursor != mouse->def_cursor) {
  1370. SDL_Cursor *found;
  1371. for (found = mouse->cursors; found; found = found->next) {
  1372. if (found == cursor) {
  1373. break;
  1374. }
  1375. }
  1376. if (!found) {
  1377. return SDL_SetError("Cursor not associated with the current mouse");
  1378. }
  1379. }
  1380. mouse->cur_cursor = cursor;
  1381. }
  1382. SDL_RedrawCursor();
  1383. return true;
  1384. }
  1385. SDL_Cursor *SDL_GetCursor(void)
  1386. {
  1387. SDL_Mouse *mouse = SDL_GetMouse();
  1388. if (!mouse) {
  1389. return NULL;
  1390. }
  1391. return mouse->cur_cursor;
  1392. }
  1393. SDL_Cursor *SDL_GetDefaultCursor(void)
  1394. {
  1395. SDL_Mouse *mouse = SDL_GetMouse();
  1396. if (!mouse) {
  1397. return NULL;
  1398. }
  1399. return mouse->def_cursor;
  1400. }
  1401. void SDL_DestroyCursor(SDL_Cursor *cursor)
  1402. {
  1403. SDL_Mouse *mouse = SDL_GetMouse();
  1404. SDL_Cursor *curr, *prev;
  1405. if (!cursor) {
  1406. return;
  1407. }
  1408. if (cursor == mouse->def_cursor) {
  1409. return;
  1410. }
  1411. if (cursor == mouse->cur_cursor) {
  1412. SDL_SetCursor(mouse->def_cursor);
  1413. }
  1414. for (prev = NULL, curr = mouse->cursors; curr;
  1415. prev = curr, curr = curr->next) {
  1416. if (curr == cursor) {
  1417. if (prev) {
  1418. prev->next = curr->next;
  1419. } else {
  1420. mouse->cursors = curr->next;
  1421. }
  1422. if (mouse->FreeCursor && curr->internal) {
  1423. mouse->FreeCursor(curr);
  1424. } else {
  1425. SDL_free(curr);
  1426. }
  1427. return;
  1428. }
  1429. }
  1430. }
  1431. bool SDL_ShowCursor(void)
  1432. {
  1433. SDL_Mouse *mouse = SDL_GetMouse();
  1434. if (mouse->warp_emulation_active) {
  1435. SDL_SetRelativeMouseMode(false);
  1436. mouse->warp_emulation_active = false;
  1437. }
  1438. if (!mouse->cursor_visible) {
  1439. mouse->cursor_visible = true;
  1440. SDL_RedrawCursor();
  1441. }
  1442. return true;
  1443. }
  1444. bool SDL_HideCursor(void)
  1445. {
  1446. SDL_Mouse *mouse = SDL_GetMouse();
  1447. if (mouse->cursor_visible) {
  1448. mouse->cursor_visible = false;
  1449. SDL_RedrawCursor();
  1450. }
  1451. return true;
  1452. }
  1453. bool SDL_CursorVisible(void)
  1454. {
  1455. SDL_Mouse *mouse = SDL_GetMouse();
  1456. return mouse->cursor_visible;
  1457. }