SDL_events.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. /* General event handling code for SDL */
  20. #include "SDL_events_c.h"
  21. #include "../SDL_hints_c.h"
  22. #include "../timer/SDL_timer_c.h"
  23. #ifndef SDL_JOYSTICK_DISABLED
  24. #include "../joystick/SDL_joystick_c.h"
  25. #endif
  26. #ifndef SDL_SENSOR_DISABLED
  27. #include "../sensor/SDL_sensor_c.h"
  28. #endif
  29. #include "../video/SDL_sysvideo.h"
  30. #include <SDL3/SDL_syswm.h>
  31. #undef SDL_PRIs64
  32. #if (defined(__WIN32__) || defined(__GDK__)) && !defined(__CYGWIN__)
  33. #define SDL_PRIs64 "I64d"
  34. #else
  35. #define SDL_PRIs64 "lld"
  36. #endif
  37. /* An arbitrary limit so we don't have unbounded growth */
  38. #define SDL_MAX_QUEUED_EVENTS 65535
  39. /* Determines how often we wake to call SDL_PumpEvents() in SDL_WaitEventTimeout_Device() */
  40. #define PERIODIC_POLL_INTERVAL_NS (3 * SDL_NS_PER_SECOND)
  41. typedef struct SDL_EventWatcher
  42. {
  43. SDL_EventFilter callback;
  44. void *userdata;
  45. SDL_bool removed;
  46. } SDL_EventWatcher;
  47. static SDL_Mutex *SDL_event_watchers_lock;
  48. static SDL_EventWatcher SDL_EventOK;
  49. static SDL_EventWatcher *SDL_event_watchers = NULL;
  50. static int SDL_event_watchers_count = 0;
  51. static SDL_bool SDL_event_watchers_dispatching = SDL_FALSE;
  52. static SDL_bool SDL_event_watchers_removed = SDL_FALSE;
  53. static SDL_AtomicInt SDL_sentinel_pending;
  54. typedef struct
  55. {
  56. Uint32 bits[8];
  57. } SDL_DisabledEventBlock;
  58. static SDL_DisabledEventBlock *SDL_disabled_events[256];
  59. static Uint32 SDL_userevents = SDL_EVENT_USER;
  60. /* Private data -- event queue */
  61. typedef struct SDL_EventEntry
  62. {
  63. SDL_Event event;
  64. SDL_SysWMmsg msg;
  65. struct SDL_EventEntry *prev;
  66. struct SDL_EventEntry *next;
  67. } SDL_EventEntry;
  68. typedef struct SDL_SysWMEntry
  69. {
  70. SDL_SysWMmsg msg;
  71. struct SDL_SysWMEntry *next;
  72. } SDL_SysWMEntry;
  73. static struct
  74. {
  75. SDL_Mutex *lock;
  76. SDL_bool active;
  77. SDL_AtomicInt count;
  78. int max_events_seen;
  79. SDL_EventEntry *head;
  80. SDL_EventEntry *tail;
  81. SDL_EventEntry *free;
  82. SDL_SysWMEntry *wmmsg_used;
  83. SDL_SysWMEntry *wmmsg_free;
  84. } SDL_EventQ = { NULL, SDL_FALSE, { 0 }, 0, NULL, NULL, NULL, NULL, NULL };
  85. #ifndef SDL_JOYSTICK_DISABLED
  86. static SDL_bool SDL_update_joysticks = SDL_TRUE;
  87. static void SDLCALL SDL_AutoUpdateJoysticksChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  88. {
  89. SDL_update_joysticks = SDL_GetStringBoolean(hint, SDL_TRUE);
  90. }
  91. #endif /* !SDL_JOYSTICK_DISABLED */
  92. #ifndef SDL_SENSOR_DISABLED
  93. static SDL_bool SDL_update_sensors = SDL_TRUE;
  94. static void SDLCALL SDL_AutoUpdateSensorsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  95. {
  96. SDL_update_sensors = SDL_GetStringBoolean(hint, SDL_TRUE);
  97. }
  98. #endif /* !SDL_SENSOR_DISABLED */
  99. static void SDLCALL SDL_PollSentinelChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  100. {
  101. SDL_SetEventEnabled(SDL_EVENT_POLL_SENTINEL, SDL_GetStringBoolean(hint, SDL_TRUE));
  102. }
  103. /**
  104. * Verbosity of logged events as defined in SDL_HINT_EVENT_LOGGING:
  105. * - 0: (default) no logging
  106. * - 1: logging of most events
  107. * - 2: as above, plus mouse and finger motion
  108. * - 3: as above, plus SDL_SysWMEvents
  109. */
  110. static int SDL_EventLoggingVerbosity = 0;
  111. static void SDLCALL SDL_EventLoggingChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  112. {
  113. SDL_EventLoggingVerbosity = (hint && *hint) ? SDL_clamp(SDL_atoi(hint), 0, 3) : 0;
  114. }
  115. static void SDL_LogEvent(const SDL_Event *event)
  116. {
  117. char name[64];
  118. char details[128];
  119. /* sensor/mouse/finger motion are spammy, ignore these if they aren't demanded. */
  120. if ((SDL_EventLoggingVerbosity < 2) &&
  121. ((event->type == SDL_EVENT_MOUSE_MOTION) ||
  122. (event->type == SDL_EVENT_FINGER_MOTION) ||
  123. (event->type == SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION) ||
  124. (event->type == SDL_EVENT_GAMEPAD_SENSOR_UPDATE) ||
  125. (event->type == SDL_EVENT_SENSOR_UPDATE))) {
  126. return;
  127. }
  128. /* window manager events are even more spammy, and don't provide much useful info. */
  129. if ((SDL_EventLoggingVerbosity < 3) && (event->type == SDL_EVENT_SYSWM)) {
  130. return;
  131. }
  132. /* this is to make (void)SDL_snprintf() calls cleaner. */
  133. #define uint unsigned int
  134. name[0] = '\0';
  135. details[0] = '\0';
  136. /* !!! FIXME: This code is kinda ugly, sorry. */
  137. if ((event->type >= SDL_EVENT_USER) && (event->type <= SDL_EVENT_LAST)) {
  138. char plusstr[16];
  139. SDL_strlcpy(name, "SDL_EVENT_USER", sizeof(name));
  140. if (event->type > SDL_EVENT_USER) {
  141. (void)SDL_snprintf(plusstr, sizeof(plusstr), "+%u", ((uint)event->type) - SDL_EVENT_USER);
  142. } else {
  143. plusstr[0] = '\0';
  144. }
  145. (void)SDL_snprintf(details, sizeof(details), "%s (timestamp=%u windowid=%u code=%d data1=%p data2=%p)",
  146. plusstr, (uint)event->user.timestamp, (uint)event->user.windowID,
  147. (int)event->user.code, event->user.data1, event->user.data2);
  148. }
  149. switch (event->type) {
  150. #define SDL_EVENT_CASE(x) \
  151. case x: \
  152. SDL_strlcpy(name, #x, sizeof(name));
  153. SDL_EVENT_CASE(SDL_EVENT_FIRST)
  154. SDL_strlcpy(details, " (THIS IS PROBABLY A BUG!)", sizeof(details));
  155. break;
  156. SDL_EVENT_CASE(SDL_EVENT_QUIT)
  157. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u)", (uint)event->quit.timestamp);
  158. break;
  159. SDL_EVENT_CASE(SDL_EVENT_TERMINATING)
  160. break;
  161. SDL_EVENT_CASE(SDL_EVENT_LOW_MEMORY)
  162. break;
  163. SDL_EVENT_CASE(SDL_EVENT_WILL_ENTER_BACKGROUND)
  164. break;
  165. SDL_EVENT_CASE(SDL_EVENT_DID_ENTER_BACKGROUND)
  166. break;
  167. SDL_EVENT_CASE(SDL_EVENT_WILL_ENTER_FOREGROUND)
  168. break;
  169. SDL_EVENT_CASE(SDL_EVENT_DID_ENTER_FOREGROUND)
  170. break;
  171. SDL_EVENT_CASE(SDL_EVENT_LOCALE_CHANGED)
  172. break;
  173. SDL_EVENT_CASE(SDL_EVENT_SYSTEM_THEME_CHANGED)
  174. break;
  175. SDL_EVENT_CASE(SDL_EVENT_KEYMAP_CHANGED)
  176. break;
  177. SDL_EVENT_CASE(SDL_EVENT_CLIPBOARD_UPDATE)
  178. break;
  179. SDL_EVENT_CASE(SDL_EVENT_RENDER_TARGETS_RESET)
  180. break;
  181. SDL_EVENT_CASE(SDL_EVENT_RENDER_DEVICE_RESET)
  182. break;
  183. #define SDL_DISPLAYEVENT_CASE(x) \
  184. case x: \
  185. SDL_strlcpy(name, #x, sizeof(name)); \
  186. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u display=%u event=%s data1=%d)", \
  187. (uint)event->display.timestamp, (uint)event->display.displayID, name, (int)event->display.data1); \
  188. break
  189. SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_ORIENTATION);
  190. SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_CONNECTED);
  191. SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_DISCONNECTED);
  192. SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_MOVED);
  193. SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED);
  194. #undef SDL_DISPLAYEVENT_CASE
  195. #define SDL_WINDOWEVENT_CASE(x) \
  196. case x: \
  197. SDL_strlcpy(name, #x, sizeof(name)); \
  198. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u event=%s data1=%d data2=%d)", \
  199. (uint)event->window.timestamp, (uint)event->window.windowID, name, (int)event->window.data1, (int)event->window.data2); \
  200. break
  201. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_SHOWN);
  202. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_HIDDEN);
  203. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_EXPOSED);
  204. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MOVED);
  205. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_RESIZED);
  206. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED);
  207. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MINIMIZED);
  208. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MAXIMIZED);
  209. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_RESTORED);
  210. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MOUSE_ENTER);
  211. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MOUSE_LEAVE);
  212. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_FOCUS_GAINED);
  213. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_FOCUS_LOST);
  214. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_CLOSE_REQUESTED);
  215. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_TAKE_FOCUS);
  216. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_HIT_TEST);
  217. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_ICCPROF_CHANGED);
  218. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_DISPLAY_CHANGED);
  219. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED);
  220. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_OCCLUDED);
  221. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_DESTROYED);
  222. #undef SDL_WINDOWEVENT_CASE
  223. SDL_EVENT_CASE(SDL_EVENT_SYSWM)
  224. /* !!! FIXME: we don't delve further at the moment. */
  225. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u)", (uint)event->syswm.timestamp);
  226. break;
  227. #define PRINT_KEY_EVENT(event) \
  228. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u state=%s repeat=%s scancode=%u keycode=%u mod=%u)", \
  229. (uint)event->key.timestamp, (uint)event->key.windowID, \
  230. event->key.state == SDL_PRESSED ? "pressed" : "released", \
  231. event->key.repeat ? "true" : "false", \
  232. (uint)event->key.keysym.scancode, \
  233. (uint)event->key.keysym.sym, \
  234. (uint)event->key.keysym.mod)
  235. SDL_EVENT_CASE(SDL_EVENT_KEY_DOWN)
  236. PRINT_KEY_EVENT(event);
  237. break;
  238. SDL_EVENT_CASE(SDL_EVENT_KEY_UP)
  239. PRINT_KEY_EVENT(event);
  240. break;
  241. #undef PRINT_KEY_EVENT
  242. SDL_EVENT_CASE(SDL_EVENT_TEXT_EDITING)
  243. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u text='%s' start=%d length=%d)",
  244. (uint)event->edit.timestamp, (uint)event->edit.windowID,
  245. event->edit.text, (int)event->edit.start, (int)event->edit.length);
  246. break;
  247. SDL_EVENT_CASE(SDL_EVENT_TEXT_INPUT)
  248. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u text='%s')", (uint)event->text.timestamp, (uint)event->text.windowID, event->text.text);
  249. break;
  250. SDL_EVENT_CASE(SDL_EVENT_MOUSE_MOTION)
  251. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u state=%u x=%g y=%g xrel=%g yrel=%g)",
  252. (uint)event->motion.timestamp, (uint)event->motion.windowID,
  253. (uint)event->motion.which, (uint)event->motion.state,
  254. event->motion.x, event->motion.y,
  255. event->motion.xrel, event->motion.yrel);
  256. break;
  257. #define PRINT_MBUTTON_EVENT(event) \
  258. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u button=%u state=%s clicks=%u x=%g y=%g)", \
  259. (uint)event->button.timestamp, (uint)event->button.windowID, \
  260. (uint)event->button.which, (uint)event->button.button, \
  261. event->button.state == SDL_PRESSED ? "pressed" : "released", \
  262. (uint)event->button.clicks, event->button.x, event->button.y)
  263. SDL_EVENT_CASE(SDL_EVENT_MOUSE_BUTTON_DOWN)
  264. PRINT_MBUTTON_EVENT(event);
  265. break;
  266. SDL_EVENT_CASE(SDL_EVENT_MOUSE_BUTTON_UP)
  267. PRINT_MBUTTON_EVENT(event);
  268. break;
  269. #undef PRINT_MBUTTON_EVENT
  270. SDL_EVENT_CASE(SDL_EVENT_MOUSE_WHEEL)
  271. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u x=%g y=%g direction=%s)",
  272. (uint)event->wheel.timestamp, (uint)event->wheel.windowID,
  273. (uint)event->wheel.which, event->wheel.x, event->wheel.y,
  274. event->wheel.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped");
  275. break;
  276. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_AXIS_MOTION)
  277. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d axis=%u value=%d)",
  278. (uint)event->jaxis.timestamp, (int)event->jaxis.which,
  279. (uint)event->jaxis.axis, (int)event->jaxis.value);
  280. break;
  281. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_HAT_MOTION)
  282. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d hat=%u value=%u)",
  283. (uint)event->jhat.timestamp, (int)event->jhat.which,
  284. (uint)event->jhat.hat, (uint)event->jhat.value);
  285. break;
  286. #define PRINT_JBUTTON_EVENT(event) \
  287. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d button=%u state=%s)", \
  288. (uint)event->jbutton.timestamp, (int)event->jbutton.which, \
  289. (uint)event->jbutton.button, event->jbutton.state == SDL_PRESSED ? "pressed" : "released")
  290. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_BUTTON_DOWN)
  291. PRINT_JBUTTON_EVENT(event);
  292. break;
  293. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_BUTTON_UP)
  294. PRINT_JBUTTON_EVENT(event);
  295. break;
  296. #undef PRINT_JBUTTON_EVENT
  297. #define PRINT_JOYDEV_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d)", (uint)event->jdevice.timestamp, (int)event->jdevice.which)
  298. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_ADDED)
  299. PRINT_JOYDEV_EVENT(event);
  300. break;
  301. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_REMOVED)
  302. PRINT_JOYDEV_EVENT(event);
  303. break;
  304. #undef PRINT_JOYDEV_EVENT
  305. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_AXIS_MOTION)
  306. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d axis=%u value=%d)",
  307. (uint)event->gaxis.timestamp, (int)event->gaxis.which,
  308. (uint)event->gaxis.axis, (int)event->gaxis.value);
  309. break;
  310. #define PRINT_CBUTTON_EVENT(event) \
  311. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d button=%u state=%s)", \
  312. (uint)event->gbutton.timestamp, (int)event->gbutton.which, \
  313. (uint)event->gbutton.button, event->gbutton.state == SDL_PRESSED ? "pressed" : "released")
  314. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_BUTTON_DOWN)
  315. PRINT_CBUTTON_EVENT(event);
  316. break;
  317. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_BUTTON_UP)
  318. PRINT_CBUTTON_EVENT(event);
  319. break;
  320. #undef PRINT_CBUTTON_EVENT
  321. #define PRINT_GAMEPADDEV_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d)", (uint)event->gdevice.timestamp, (int)event->gdevice.which)
  322. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_ADDED)
  323. PRINT_GAMEPADDEV_EVENT(event);
  324. break;
  325. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_REMOVED)
  326. PRINT_GAMEPADDEV_EVENT(event);
  327. break;
  328. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_REMAPPED)
  329. PRINT_GAMEPADDEV_EVENT(event);
  330. break;
  331. #undef PRINT_GAMEPADDEV_EVENT
  332. #define PRINT_CTOUCHPAD_EVENT(event) \
  333. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d touchpad=%d finger=%d x=%f y=%f pressure=%f)", \
  334. (uint)event->gtouchpad.timestamp, (int)event->gtouchpad.which, \
  335. (int)event->gtouchpad.touchpad, (int)event->gtouchpad.finger, \
  336. event->gtouchpad.x, event->gtouchpad.y, event->gtouchpad.pressure)
  337. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN)
  338. PRINT_CTOUCHPAD_EVENT(event);
  339. break;
  340. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_TOUCHPAD_UP)
  341. PRINT_CTOUCHPAD_EVENT(event);
  342. break;
  343. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION)
  344. PRINT_CTOUCHPAD_EVENT(event);
  345. break;
  346. #undef PRINT_CTOUCHPAD_EVENT
  347. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_SENSOR_UPDATE)
  348. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d sensor=%d data[0]=%f data[1]=%f data[2]=%f)",
  349. (uint)event->gsensor.timestamp, (int)event->gsensor.which, (int)event->gsensor.sensor,
  350. event->gsensor.data[0], event->gsensor.data[1], event->gsensor.data[2]);
  351. break;
  352. #define PRINT_FINGER_EVENT(event) \
  353. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u touchid=%" SDL_PRIs64 " fingerid=%" SDL_PRIs64 " x=%f y=%f dx=%f dy=%f pressure=%f)", \
  354. (uint)event->tfinger.timestamp, (long long)event->tfinger.touchId, \
  355. (long long)event->tfinger.fingerId, event->tfinger.x, event->tfinger.y, \
  356. event->tfinger.dx, event->tfinger.dy, event->tfinger.pressure)
  357. SDL_EVENT_CASE(SDL_EVENT_FINGER_DOWN)
  358. PRINT_FINGER_EVENT(event);
  359. break;
  360. SDL_EVENT_CASE(SDL_EVENT_FINGER_UP)
  361. PRINT_FINGER_EVENT(event);
  362. break;
  363. SDL_EVENT_CASE(SDL_EVENT_FINGER_MOTION)
  364. PRINT_FINGER_EVENT(event);
  365. break;
  366. #undef PRINT_FINGER_EVENT
  367. #define PRINT_DROP_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (file='%s' timestamp=%u windowid=%u)", event->drop.file, (uint)event->drop.timestamp, (uint)event->drop.windowID)
  368. SDL_EVENT_CASE(SDL_EVENT_DROP_FILE)
  369. PRINT_DROP_EVENT(event);
  370. break;
  371. SDL_EVENT_CASE(SDL_EVENT_DROP_TEXT)
  372. PRINT_DROP_EVENT(event);
  373. break;
  374. SDL_EVENT_CASE(SDL_EVENT_DROP_BEGIN)
  375. PRINT_DROP_EVENT(event);
  376. break;
  377. SDL_EVENT_CASE(SDL_EVENT_DROP_COMPLETE)
  378. PRINT_DROP_EVENT(event);
  379. break;
  380. #undef PRINT_DROP_EVENT
  381. #define PRINT_AUDIODEV_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%u iscapture=%s)", (uint)event->adevice.timestamp, (uint)event->adevice.which, event->adevice.iscapture ? "true" : "false")
  382. SDL_EVENT_CASE(SDL_EVENT_AUDIO_DEVICE_ADDED)
  383. PRINT_AUDIODEV_EVENT(event);
  384. break;
  385. SDL_EVENT_CASE(SDL_EVENT_AUDIO_DEVICE_REMOVED)
  386. PRINT_AUDIODEV_EVENT(event);
  387. break;
  388. #undef PRINT_AUDIODEV_EVENT
  389. SDL_EVENT_CASE(SDL_EVENT_SENSOR_UPDATE)
  390. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d data[0]=%f data[1]=%f data[2]=%f data[3]=%f data[4]=%f data[5]=%f)",
  391. (uint)event->sensor.timestamp, (int)event->sensor.which,
  392. event->sensor.data[0], event->sensor.data[1], event->sensor.data[2],
  393. event->sensor.data[3], event->sensor.data[4], event->sensor.data[5]);
  394. break;
  395. #undef SDL_EVENT_CASE
  396. case SDL_EVENT_POLL_SENTINEL:
  397. /* No logging necessary for this one */
  398. break;
  399. default:
  400. if (!name[0]) {
  401. SDL_strlcpy(name, "UNKNOWN", sizeof(name));
  402. (void)SDL_snprintf(details, sizeof(details), " #%u! (Bug? FIXME?)", (uint)event->type);
  403. }
  404. break;
  405. }
  406. if (name[0]) {
  407. SDL_Log("SDL EVENT: %s%s", name, details);
  408. }
  409. #undef uint
  410. }
  411. /* Public functions */
  412. void SDL_StopEventLoop(void)
  413. {
  414. const char *report = SDL_GetHint("SDL_EVENT_QUEUE_STATISTICS");
  415. int i;
  416. SDL_EventEntry *entry;
  417. SDL_SysWMEntry *wmmsg;
  418. SDL_LockMutex(SDL_EventQ.lock);
  419. SDL_EventQ.active = SDL_FALSE;
  420. if (report && SDL_atoi(report)) {
  421. SDL_Log("SDL EVENT QUEUE: Maximum events in-flight: %d\n",
  422. SDL_EventQ.max_events_seen);
  423. }
  424. /* Clean out EventQ */
  425. for (entry = SDL_EventQ.head; entry;) {
  426. SDL_EventEntry *next = entry->next;
  427. SDL_free(entry);
  428. entry = next;
  429. }
  430. for (entry = SDL_EventQ.free; entry;) {
  431. SDL_EventEntry *next = entry->next;
  432. SDL_free(entry);
  433. entry = next;
  434. }
  435. for (wmmsg = SDL_EventQ.wmmsg_used; wmmsg;) {
  436. SDL_SysWMEntry *next = wmmsg->next;
  437. SDL_free(wmmsg);
  438. wmmsg = next;
  439. }
  440. for (wmmsg = SDL_EventQ.wmmsg_free; wmmsg;) {
  441. SDL_SysWMEntry *next = wmmsg->next;
  442. SDL_free(wmmsg);
  443. wmmsg = next;
  444. }
  445. SDL_AtomicSet(&SDL_EventQ.count, 0);
  446. SDL_EventQ.max_events_seen = 0;
  447. SDL_EventQ.head = NULL;
  448. SDL_EventQ.tail = NULL;
  449. SDL_EventQ.free = NULL;
  450. SDL_EventQ.wmmsg_used = NULL;
  451. SDL_EventQ.wmmsg_free = NULL;
  452. SDL_AtomicSet(&SDL_sentinel_pending, 0);
  453. /* Clear disabled event state */
  454. for (i = 0; i < SDL_arraysize(SDL_disabled_events); ++i) {
  455. SDL_free(SDL_disabled_events[i]);
  456. SDL_disabled_events[i] = NULL;
  457. }
  458. if (SDL_event_watchers_lock) {
  459. SDL_DestroyMutex(SDL_event_watchers_lock);
  460. SDL_event_watchers_lock = NULL;
  461. }
  462. if (SDL_event_watchers) {
  463. SDL_free(SDL_event_watchers);
  464. SDL_event_watchers = NULL;
  465. SDL_event_watchers_count = 0;
  466. }
  467. SDL_zero(SDL_EventOK);
  468. SDL_UnlockMutex(SDL_EventQ.lock);
  469. if (SDL_EventQ.lock) {
  470. SDL_DestroyMutex(SDL_EventQ.lock);
  471. SDL_EventQ.lock = NULL;
  472. }
  473. }
  474. /* This function (and associated calls) may be called more than once */
  475. int SDL_StartEventLoop(void)
  476. {
  477. /* We'll leave the event queue alone, since we might have gotten
  478. some important events at launch (like SDL_EVENT_DROP_FILE)
  479. FIXME: Does this introduce any other bugs with events at startup?
  480. */
  481. /* Create the lock and set ourselves active */
  482. #ifndef SDL_THREADS_DISABLED
  483. if (!SDL_EventQ.lock) {
  484. SDL_EventQ.lock = SDL_CreateMutex();
  485. if (SDL_EventQ.lock == NULL) {
  486. return -1;
  487. }
  488. }
  489. SDL_LockMutex(SDL_EventQ.lock);
  490. if (SDL_event_watchers_lock == NULL) {
  491. SDL_event_watchers_lock = SDL_CreateMutex();
  492. if (SDL_event_watchers_lock == NULL) {
  493. SDL_UnlockMutex(SDL_EventQ.lock);
  494. return -1;
  495. }
  496. }
  497. #endif /* !SDL_THREADS_DISABLED */
  498. /* Process most event types */
  499. SDL_SetEventEnabled(SDL_EVENT_TEXT_INPUT, SDL_FALSE);
  500. SDL_SetEventEnabled(SDL_EVENT_TEXT_EDITING, SDL_FALSE);
  501. SDL_SetEventEnabled(SDL_EVENT_SYSWM, SDL_FALSE);
  502. #if 0 /* Leave these events enabled so apps can respond to items being dragged onto them at startup */
  503. SDL_SetEventEnabled(SDL_EVENT_DROP_FILE, SDL_FALSE);
  504. SDL_SetEventEnabled(SDL_EVENT_DROP_TEXT, SDL_FALSE);
  505. #endif
  506. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_UPDATE_COMPLETE, SDL_FALSE);
  507. SDL_SetEventEnabled(SDL_EVENT_GAMEPAD_UPDATE_COMPLETE, SDL_FALSE);
  508. SDL_EventQ.active = SDL_TRUE;
  509. SDL_UnlockMutex(SDL_EventQ.lock);
  510. return 0;
  511. }
  512. /* Add an event to the event queue -- called with the queue locked */
  513. static int SDL_AddEvent(SDL_Event *event)
  514. {
  515. SDL_EventEntry *entry;
  516. const int initial_count = SDL_AtomicGet(&SDL_EventQ.count);
  517. int final_count;
  518. if (initial_count >= SDL_MAX_QUEUED_EVENTS) {
  519. SDL_SetError("Event queue is full (%d events)", initial_count);
  520. return 0;
  521. }
  522. if (SDL_EventQ.free == NULL) {
  523. entry = (SDL_EventEntry *)SDL_malloc(sizeof(*entry));
  524. if (entry == NULL) {
  525. return 0;
  526. }
  527. } else {
  528. entry = SDL_EventQ.free;
  529. SDL_EventQ.free = entry->next;
  530. }
  531. if (SDL_EventLoggingVerbosity > 0) {
  532. SDL_LogEvent(event);
  533. }
  534. entry->event = *event;
  535. if (event->type == SDL_EVENT_POLL_SENTINEL) {
  536. SDL_AtomicAdd(&SDL_sentinel_pending, 1);
  537. } else if (event->type == SDL_EVENT_SYSWM) {
  538. entry->msg = *event->syswm.msg;
  539. entry->event.syswm.msg = &entry->msg;
  540. }
  541. if (SDL_EventQ.tail) {
  542. SDL_EventQ.tail->next = entry;
  543. entry->prev = SDL_EventQ.tail;
  544. SDL_EventQ.tail = entry;
  545. entry->next = NULL;
  546. } else {
  547. SDL_assert(!SDL_EventQ.head);
  548. SDL_EventQ.head = entry;
  549. SDL_EventQ.tail = entry;
  550. entry->prev = NULL;
  551. entry->next = NULL;
  552. }
  553. final_count = SDL_AtomicAdd(&SDL_EventQ.count, 1) + 1;
  554. if (final_count > SDL_EventQ.max_events_seen) {
  555. SDL_EventQ.max_events_seen = final_count;
  556. }
  557. return 1;
  558. }
  559. /* Remove an event from the queue -- called with the queue locked */
  560. static void SDL_CutEvent(SDL_EventEntry *entry)
  561. {
  562. if (entry->prev) {
  563. entry->prev->next = entry->next;
  564. }
  565. if (entry->next) {
  566. entry->next->prev = entry->prev;
  567. }
  568. if (entry == SDL_EventQ.head) {
  569. SDL_assert(entry->prev == NULL);
  570. SDL_EventQ.head = entry->next;
  571. }
  572. if (entry == SDL_EventQ.tail) {
  573. SDL_assert(entry->next == NULL);
  574. SDL_EventQ.tail = entry->prev;
  575. }
  576. if (entry->event.type == SDL_EVENT_POLL_SENTINEL) {
  577. SDL_AtomicAdd(&SDL_sentinel_pending, -1);
  578. }
  579. entry->next = SDL_EventQ.free;
  580. SDL_EventQ.free = entry;
  581. SDL_assert(SDL_AtomicGet(&SDL_EventQ.count) > 0);
  582. SDL_AtomicAdd(&SDL_EventQ.count, -1);
  583. }
  584. static int SDL_SendWakeupEvent(void)
  585. {
  586. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  587. if (_this == NULL || !_this->SendWakeupEvent) {
  588. return 0;
  589. }
  590. SDL_LockMutex(_this->wakeup_lock);
  591. {
  592. if (_this->wakeup_window) {
  593. _this->SendWakeupEvent(_this, _this->wakeup_window);
  594. /* No more wakeup events needed until we enter a new wait */
  595. _this->wakeup_window = NULL;
  596. }
  597. }
  598. SDL_UnlockMutex(_this->wakeup_lock);
  599. return 0;
  600. }
  601. /* Lock the event queue, take a peep at it, and unlock it */
  602. static int SDL_PeepEventsInternal(SDL_Event *events, int numevents, SDL_eventaction action,
  603. Uint32 minType, Uint32 maxType, SDL_bool include_sentinel)
  604. {
  605. int i, used, sentinels_expected = 0;
  606. /* Lock the event queue */
  607. used = 0;
  608. SDL_LockMutex(SDL_EventQ.lock);
  609. {
  610. /* Don't look after we've quit */
  611. if (!SDL_EventQ.active) {
  612. /* We get a few spurious events at shutdown, so don't warn then */
  613. if (action == SDL_GETEVENT) {
  614. SDL_SetError("The event system has been shut down");
  615. }
  616. SDL_UnlockMutex(SDL_EventQ.lock);
  617. return -1;
  618. }
  619. if (action == SDL_ADDEVENT) {
  620. for (i = 0; i < numevents; ++i) {
  621. used += SDL_AddEvent(&events[i]);
  622. }
  623. } else {
  624. SDL_EventEntry *entry, *next;
  625. SDL_SysWMEntry *wmmsg, *wmmsg_next;
  626. Uint32 type;
  627. if (action == SDL_GETEVENT) {
  628. /* Clean out any used wmmsg data
  629. FIXME: Do we want to retain the data for some period of time?
  630. */
  631. for (wmmsg = SDL_EventQ.wmmsg_used; wmmsg; wmmsg = wmmsg_next) {
  632. wmmsg_next = wmmsg->next;
  633. wmmsg->next = SDL_EventQ.wmmsg_free;
  634. SDL_EventQ.wmmsg_free = wmmsg;
  635. }
  636. SDL_EventQ.wmmsg_used = NULL;
  637. }
  638. for (entry = SDL_EventQ.head; entry && (events == NULL || used < numevents); entry = next) {
  639. next = entry->next;
  640. type = entry->event.type;
  641. if (minType <= type && type <= maxType) {
  642. if (events) {
  643. events[used] = entry->event;
  644. if (entry->event.type == SDL_EVENT_SYSWM) {
  645. /* We need to copy the wmmsg somewhere safe.
  646. For now we'll guarantee it's valid at least until
  647. the next call to SDL_PeepEvents()
  648. */
  649. if (SDL_EventQ.wmmsg_free) {
  650. wmmsg = SDL_EventQ.wmmsg_free;
  651. SDL_EventQ.wmmsg_free = wmmsg->next;
  652. } else {
  653. wmmsg = (SDL_SysWMEntry *)SDL_malloc(sizeof(*wmmsg));
  654. }
  655. wmmsg->msg = *entry->event.syswm.msg;
  656. wmmsg->next = SDL_EventQ.wmmsg_used;
  657. SDL_EventQ.wmmsg_used = wmmsg;
  658. events[used].syswm.msg = &wmmsg->msg;
  659. }
  660. if (action == SDL_GETEVENT) {
  661. SDL_CutEvent(entry);
  662. }
  663. }
  664. if (type == SDL_EVENT_POLL_SENTINEL) {
  665. /* Special handling for the sentinel event */
  666. if (!include_sentinel) {
  667. /* Skip it, we don't want to include it */
  668. continue;
  669. }
  670. if (events == NULL || action != SDL_GETEVENT) {
  671. ++sentinels_expected;
  672. }
  673. if (SDL_AtomicGet(&SDL_sentinel_pending) > sentinels_expected) {
  674. /* Skip it, there's another one pending */
  675. continue;
  676. }
  677. }
  678. ++used;
  679. }
  680. }
  681. }
  682. }
  683. SDL_UnlockMutex(SDL_EventQ.lock);
  684. if (used > 0 && action == SDL_ADDEVENT) {
  685. SDL_SendWakeupEvent();
  686. }
  687. return used;
  688. }
  689. int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction action,
  690. Uint32 minType, Uint32 maxType)
  691. {
  692. return SDL_PeepEventsInternal(events, numevents, action, minType, maxType, SDL_FALSE);
  693. }
  694. SDL_bool SDL_HasEvent(Uint32 type)
  695. {
  696. return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type) > 0;
  697. }
  698. SDL_bool SDL_HasEvents(Uint32 minType, Uint32 maxType)
  699. {
  700. return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, minType, maxType) > 0;
  701. }
  702. void SDL_FlushEvent(Uint32 type)
  703. {
  704. SDL_FlushEvents(type, type);
  705. }
  706. void SDL_FlushEvents(Uint32 minType, Uint32 maxType)
  707. {
  708. SDL_EventEntry *entry, *next;
  709. Uint32 type;
  710. /* !!! FIXME: we need to manually SDL_free() the strings in TEXTINPUT and
  711. drag'n'drop events if we're flushing them without passing them to the
  712. app, but I don't know if this is the right place to do that. */
  713. /* Make sure the events are current */
  714. #if 0
  715. /* Actually, we can't do this since we might be flushing while processing
  716. a resize event, and calling this might trigger further resize events.
  717. */
  718. SDL_PumpEvents();
  719. #endif
  720. /* Lock the event queue */
  721. SDL_LockMutex(SDL_EventQ.lock);
  722. {
  723. /* Don't look after we've quit */
  724. if (!SDL_EventQ.active) {
  725. SDL_UnlockMutex(SDL_EventQ.lock);
  726. return;
  727. }
  728. for (entry = SDL_EventQ.head; entry; entry = next) {
  729. next = entry->next;
  730. type = entry->event.type;
  731. if (minType <= type && type <= maxType) {
  732. SDL_CutEvent(entry);
  733. }
  734. }
  735. }
  736. SDL_UnlockMutex(SDL_EventQ.lock);
  737. }
  738. /* Run the system dependent event loops */
  739. static void SDL_PumpEventsInternal(SDL_bool push_sentinel)
  740. {
  741. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  742. /* Release any keys held down from last frame */
  743. SDL_ReleaseAutoReleaseKeys();
  744. /* Get events from the video subsystem */
  745. if (_this) {
  746. _this->PumpEvents(_this);
  747. }
  748. #ifndef SDL_SENSOR_DISABLED
  749. /* Check for sensor state change */
  750. if (SDL_update_sensors) {
  751. SDL_UpdateSensors();
  752. }
  753. #endif
  754. #ifndef SDL_JOYSTICK_DISABLED
  755. /* Check for joystick state change */
  756. if (SDL_update_joysticks) {
  757. SDL_UpdateJoysticks();
  758. }
  759. #endif
  760. SDL_SendPendingSignalEvents(); /* in case we had a signal handler fire, etc. */
  761. if (push_sentinel && SDL_EventEnabled(SDL_EVENT_POLL_SENTINEL)) {
  762. SDL_Event sentinel;
  763. /* Make sure we don't already have a sentinel in the queue, and add one to the end */
  764. if (SDL_AtomicGet(&SDL_sentinel_pending) > 0) {
  765. SDL_PeepEventsInternal(&sentinel, 1, SDL_GETEVENT, SDL_EVENT_POLL_SENTINEL, SDL_EVENT_POLL_SENTINEL, SDL_TRUE);
  766. }
  767. sentinel.type = SDL_EVENT_POLL_SENTINEL;
  768. sentinel.common.timestamp = 0;
  769. SDL_PushEvent(&sentinel);
  770. }
  771. }
  772. void SDL_PumpEvents(void)
  773. {
  774. SDL_PumpEventsInternal(SDL_FALSE);
  775. }
  776. /* Public functions */
  777. int SDL_PollEvent(SDL_Event *event)
  778. {
  779. return SDL_WaitEventTimeoutNS(event, 0);
  780. }
  781. static SDL_bool SDL_events_need_periodic_poll(void)
  782. {
  783. SDL_bool need_periodic_poll = SDL_FALSE;
  784. #ifndef SDL_JOYSTICK_DISABLED
  785. need_periodic_poll = SDL_WasInit(SDL_INIT_JOYSTICK) && SDL_update_joysticks;
  786. #endif
  787. return need_periodic_poll;
  788. }
  789. static int SDL_WaitEventTimeout_Device(SDL_VideoDevice *_this, SDL_Window *wakeup_window, SDL_Event *event, Uint64 start, Sint64 timeoutNS)
  790. {
  791. Sint64 loop_timeoutNS = timeoutNS;
  792. SDL_bool need_periodic_poll = SDL_events_need_periodic_poll();
  793. for (;;) {
  794. int status;
  795. /* Pump events on entry and each time we wake to ensure:
  796. a) All pending events are batch processed after waking up from a wait
  797. b) Waiting can be completely skipped if events are already available to be pumped
  798. c) Periodic processing that takes place in some platform PumpEvents() functions happens
  799. d) Signals received in WaitEventTimeout() are turned into SDL events
  800. */
  801. SDL_PumpEventsInternal(SDL_TRUE);
  802. SDL_LockMutex(_this->wakeup_lock);
  803. {
  804. status = SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST);
  805. /* If status == 0 we are going to block so wakeup will be needed. */
  806. if (status == 0) {
  807. _this->wakeup_window = wakeup_window;
  808. } else {
  809. _this->wakeup_window = NULL;
  810. }
  811. }
  812. SDL_UnlockMutex(_this->wakeup_lock);
  813. if (status < 0) {
  814. /* Got an error: return */
  815. break;
  816. }
  817. if (status > 0) {
  818. /* There is an event, we can return. */
  819. return 1;
  820. }
  821. /* No events found in the queue, call WaitEventTimeout to wait for an event. */
  822. if (timeoutNS > 0) {
  823. Sint64 elapsed = SDL_GetTicksNS() - start;
  824. if (elapsed >= timeoutNS) {
  825. /* Set wakeup_window to NULL without holding the lock. */
  826. _this->wakeup_window = NULL;
  827. return 0;
  828. }
  829. loop_timeoutNS = (timeoutNS - elapsed);
  830. }
  831. if (need_periodic_poll) {
  832. if (loop_timeoutNS >= 0) {
  833. loop_timeoutNS = SDL_min(loop_timeoutNS, PERIODIC_POLL_INTERVAL_NS);
  834. } else {
  835. loop_timeoutNS = PERIODIC_POLL_INTERVAL_NS;
  836. }
  837. }
  838. status = _this->WaitEventTimeout(_this, loop_timeoutNS);
  839. /* Set wakeup_window to NULL without holding the lock. */
  840. _this->wakeup_window = NULL;
  841. if (status == 0 && need_periodic_poll && loop_timeoutNS == PERIODIC_POLL_INTERVAL_NS) {
  842. /* We may have woken up to poll. Try again */
  843. continue;
  844. } else if (status <= 0) {
  845. /* There is either an error or the timeout is elapsed: return */
  846. return status;
  847. }
  848. /* An event was found and pumped into the SDL events queue. Continue the loop
  849. to let SDL_PeepEvents pick it up .*/
  850. }
  851. return 0;
  852. }
  853. static SDL_bool SDL_events_need_polling(void)
  854. {
  855. SDL_bool need_polling = SDL_FALSE;
  856. #ifndef SDL_JOYSTICK_DISABLED
  857. need_polling = SDL_WasInit(SDL_INIT_JOYSTICK) && SDL_update_joysticks && SDL_JoysticksOpened();
  858. #endif
  859. #ifndef SDL_SENSOR_DISABLED
  860. need_polling = need_polling ||
  861. (SDL_WasInit(SDL_INIT_SENSOR) && SDL_update_sensors && SDL_SensorsOpened());
  862. #endif
  863. return need_polling;
  864. }
  865. static SDL_Window *SDL_find_active_window(SDL_VideoDevice *_this)
  866. {
  867. SDL_Window *window;
  868. for (window = _this->windows; window; window = window->next) {
  869. if (!window->is_destroying) {
  870. return window;
  871. }
  872. }
  873. return NULL;
  874. }
  875. int SDL_WaitEvent(SDL_Event *event)
  876. {
  877. return SDL_WaitEventTimeoutNS(event, -1);
  878. }
  879. int SDL_WaitEventTimeout(SDL_Event *event, Sint32 timeoutMS)
  880. {
  881. Uint64 timeoutNS;
  882. if (timeoutMS > 0) {
  883. timeoutNS = SDL_MS_TO_NS(timeoutMS);
  884. } else {
  885. timeoutNS = timeoutMS;
  886. }
  887. return SDL_WaitEventTimeoutNS(event, timeoutNS);
  888. }
  889. int SDL_WaitEventTimeoutNS(SDL_Event *event, Sint64 timeoutNS)
  890. {
  891. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  892. SDL_Window *wakeup_window;
  893. Uint64 start, expiration;
  894. SDL_bool include_sentinel = (timeoutNS == 0) ? SDL_TRUE : SDL_FALSE;
  895. int result;
  896. /* If there isn't a poll sentinel event pending, pump events and add one */
  897. if (SDL_AtomicGet(&SDL_sentinel_pending) == 0) {
  898. SDL_PumpEventsInternal(SDL_TRUE);
  899. }
  900. /* First check for existing events */
  901. result = SDL_PeepEventsInternal(event, 1, SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST, include_sentinel);
  902. if (result < 0) {
  903. return 0;
  904. }
  905. if (include_sentinel) {
  906. if (event) {
  907. if (event->type == SDL_EVENT_POLL_SENTINEL) {
  908. /* Reached the end of a poll cycle, and not willing to wait */
  909. return 0;
  910. }
  911. } else {
  912. /* Need to peek the next event to check for sentinel */
  913. SDL_Event dummy;
  914. if (SDL_PeepEventsInternal(&dummy, 1, SDL_PEEKEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST, SDL_TRUE) &&
  915. dummy.type == SDL_EVENT_POLL_SENTINEL) {
  916. SDL_PeepEventsInternal(&dummy, 1, SDL_GETEVENT, SDL_EVENT_POLL_SENTINEL, SDL_EVENT_POLL_SENTINEL, SDL_TRUE);
  917. /* Reached the end of a poll cycle, and not willing to wait */
  918. return 0;
  919. }
  920. }
  921. }
  922. if (result == 0) {
  923. if (timeoutNS == 0) {
  924. /* No events available, and not willing to wait */
  925. return 0;
  926. }
  927. } else {
  928. /* Has existing events */
  929. return 1;
  930. }
  931. /* We should have completely handled timeoutNS == 0 above */
  932. SDL_assert(timeoutNS != 0);
  933. if (timeoutNS > 0) {
  934. start = SDL_GetTicksNS();
  935. expiration = start + timeoutNS;
  936. } else {
  937. start = 0;
  938. expiration = 0;
  939. }
  940. if (_this && _this->WaitEventTimeout && _this->SendWakeupEvent && !SDL_events_need_polling()) {
  941. /* Look if a shown window is available to send the wakeup event. */
  942. wakeup_window = SDL_find_active_window(_this);
  943. if (wakeup_window) {
  944. int status = SDL_WaitEventTimeout_Device(_this, wakeup_window, event, start, timeoutNS);
  945. /* There may be implementation-defined conditions where the backend cannot
  946. reliably wait for the next event. If that happens, fall back to polling. */
  947. if (status >= 0) {
  948. return status;
  949. }
  950. }
  951. }
  952. for (;;) {
  953. SDL_PumpEventsInternal(SDL_TRUE);
  954. switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST)) {
  955. case -1:
  956. return 0;
  957. case 0:
  958. if (timeoutNS > 0 && SDL_GetTicksNS() >= expiration) {
  959. /* Timeout expired and no events */
  960. return 0;
  961. }
  962. SDL_Delay(1);
  963. break;
  964. default:
  965. /* Has events */
  966. return 1;
  967. }
  968. }
  969. }
  970. int SDL_PushEvent(SDL_Event *event)
  971. {
  972. if (!event->common.timestamp) {
  973. event->common.timestamp = SDL_GetTicksNS();
  974. }
  975. if (SDL_EventOK.callback || SDL_event_watchers_count > 0) {
  976. SDL_LockMutex(SDL_event_watchers_lock);
  977. {
  978. if (SDL_EventOK.callback && !SDL_EventOK.callback(SDL_EventOK.userdata, event)) {
  979. SDL_UnlockMutex(SDL_event_watchers_lock);
  980. return 0;
  981. }
  982. if (SDL_event_watchers_count > 0) {
  983. /* Make sure we only dispatch the current watcher list */
  984. int i, event_watchers_count = SDL_event_watchers_count;
  985. SDL_event_watchers_dispatching = SDL_TRUE;
  986. for (i = 0; i < event_watchers_count; ++i) {
  987. if (!SDL_event_watchers[i].removed) {
  988. SDL_event_watchers[i].callback(SDL_event_watchers[i].userdata, event);
  989. }
  990. }
  991. SDL_event_watchers_dispatching = SDL_FALSE;
  992. if (SDL_event_watchers_removed) {
  993. for (i = SDL_event_watchers_count; i--;) {
  994. if (SDL_event_watchers[i].removed) {
  995. --SDL_event_watchers_count;
  996. if (i < SDL_event_watchers_count) {
  997. SDL_memmove(&SDL_event_watchers[i], &SDL_event_watchers[i + 1], (SDL_event_watchers_count - i) * sizeof(SDL_event_watchers[i]));
  998. }
  999. }
  1000. }
  1001. SDL_event_watchers_removed = SDL_FALSE;
  1002. }
  1003. }
  1004. }
  1005. SDL_UnlockMutex(SDL_event_watchers_lock);
  1006. }
  1007. if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
  1008. return -1;
  1009. }
  1010. return 1;
  1011. }
  1012. void SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
  1013. {
  1014. SDL_LockMutex(SDL_event_watchers_lock);
  1015. {
  1016. /* Set filter and discard pending events */
  1017. SDL_EventOK.callback = filter;
  1018. SDL_EventOK.userdata = userdata;
  1019. SDL_FlushEvents(SDL_EVENT_FIRST, SDL_EVENT_LAST);
  1020. }
  1021. SDL_UnlockMutex(SDL_event_watchers_lock);
  1022. }
  1023. SDL_bool SDL_GetEventFilter(SDL_EventFilter *filter, void **userdata)
  1024. {
  1025. SDL_EventWatcher event_ok;
  1026. SDL_LockMutex(SDL_event_watchers_lock);
  1027. {
  1028. event_ok = SDL_EventOK;
  1029. }
  1030. SDL_UnlockMutex(SDL_event_watchers_lock);
  1031. if (filter) {
  1032. *filter = event_ok.callback;
  1033. }
  1034. if (userdata) {
  1035. *userdata = event_ok.userdata;
  1036. }
  1037. return event_ok.callback ? SDL_TRUE : SDL_FALSE;
  1038. }
  1039. void SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
  1040. {
  1041. SDL_LockMutex(SDL_event_watchers_lock);
  1042. {
  1043. SDL_EventWatcher *event_watchers;
  1044. event_watchers = SDL_realloc(SDL_event_watchers, (SDL_event_watchers_count + 1) * sizeof(*event_watchers));
  1045. if (event_watchers) {
  1046. SDL_EventWatcher *watcher;
  1047. SDL_event_watchers = event_watchers;
  1048. watcher = &SDL_event_watchers[SDL_event_watchers_count];
  1049. watcher->callback = filter;
  1050. watcher->userdata = userdata;
  1051. watcher->removed = SDL_FALSE;
  1052. ++SDL_event_watchers_count;
  1053. }
  1054. }
  1055. SDL_UnlockMutex(SDL_event_watchers_lock);
  1056. }
  1057. void SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
  1058. {
  1059. SDL_LockMutex(SDL_event_watchers_lock);
  1060. {
  1061. int i;
  1062. for (i = 0; i < SDL_event_watchers_count; ++i) {
  1063. if (SDL_event_watchers[i].callback == filter && SDL_event_watchers[i].userdata == userdata) {
  1064. if (SDL_event_watchers_dispatching) {
  1065. SDL_event_watchers[i].removed = SDL_TRUE;
  1066. SDL_event_watchers_removed = SDL_TRUE;
  1067. } else {
  1068. --SDL_event_watchers_count;
  1069. if (i < SDL_event_watchers_count) {
  1070. SDL_memmove(&SDL_event_watchers[i], &SDL_event_watchers[i + 1], (SDL_event_watchers_count - i) * sizeof(SDL_event_watchers[i]));
  1071. }
  1072. }
  1073. break;
  1074. }
  1075. }
  1076. }
  1077. SDL_UnlockMutex(SDL_event_watchers_lock);
  1078. }
  1079. void SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
  1080. {
  1081. SDL_LockMutex(SDL_EventQ.lock);
  1082. {
  1083. SDL_EventEntry *entry, *next;
  1084. for (entry = SDL_EventQ.head; entry; entry = next) {
  1085. next = entry->next;
  1086. if (!filter(userdata, &entry->event)) {
  1087. SDL_CutEvent(entry);
  1088. }
  1089. }
  1090. }
  1091. SDL_UnlockMutex(SDL_EventQ.lock);
  1092. }
  1093. void SDL_SetEventEnabled(Uint32 type, SDL_bool enabled)
  1094. {
  1095. SDL_bool current_state;
  1096. Uint8 hi = ((type >> 8) & 0xff);
  1097. Uint8 lo = (type & 0xff);
  1098. if (SDL_disabled_events[hi] &&
  1099. (SDL_disabled_events[hi]->bits[lo / 32] & (1 << (lo & 31)))) {
  1100. current_state = SDL_FALSE;
  1101. } else {
  1102. current_state = SDL_TRUE;
  1103. }
  1104. if (enabled != current_state) {
  1105. if (enabled) {
  1106. SDL_disabled_events[hi]->bits[lo / 32] &= ~(1 << (lo & 31));
  1107. /* Gamepad events depend on joystick events */
  1108. switch (type) {
  1109. case SDL_EVENT_GAMEPAD_ADDED:
  1110. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_ADDED, SDL_TRUE);
  1111. break;
  1112. case SDL_EVENT_GAMEPAD_REMOVED:
  1113. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_REMOVED, SDL_TRUE);
  1114. break;
  1115. case SDL_EVENT_GAMEPAD_AXIS_MOTION:
  1116. case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
  1117. case SDL_EVENT_GAMEPAD_BUTTON_UP:
  1118. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_AXIS_MOTION, SDL_TRUE);
  1119. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_HAT_MOTION, SDL_TRUE);
  1120. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_BUTTON_DOWN, SDL_TRUE);
  1121. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_BUTTON_UP, SDL_TRUE);
  1122. break;
  1123. case SDL_EVENT_GAMEPAD_UPDATE_COMPLETE:
  1124. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_UPDATE_COMPLETE, SDL_TRUE);
  1125. break;
  1126. default:
  1127. break;
  1128. }
  1129. } else {
  1130. /* Disable this event type and discard pending events */
  1131. if (!SDL_disabled_events[hi]) {
  1132. SDL_disabled_events[hi] = (SDL_DisabledEventBlock *)SDL_calloc(1, sizeof(SDL_DisabledEventBlock));
  1133. }
  1134. /* Out of memory, nothing we can do... */
  1135. if (SDL_disabled_events[hi]) {
  1136. SDL_disabled_events[hi]->bits[lo / 32] |= (1 << (lo & 31));
  1137. SDL_FlushEvent(type);
  1138. }
  1139. }
  1140. /* turn off drag'n'drop support if we've disabled the events.
  1141. This might change some UI details at the OS level. */
  1142. if (type == SDL_EVENT_DROP_FILE || type == SDL_EVENT_DROP_TEXT) {
  1143. SDL_ToggleDragAndDropSupport();
  1144. }
  1145. }
  1146. }
  1147. SDL_bool SDL_EventEnabled(Uint32 type)
  1148. {
  1149. Uint8 hi = ((type >> 8) & 0xff);
  1150. Uint8 lo = (type & 0xff);
  1151. if (SDL_disabled_events[hi] &&
  1152. (SDL_disabled_events[hi]->bits[lo / 32] & (1 << (lo & 31)))) {
  1153. return SDL_FALSE;
  1154. } else {
  1155. return SDL_TRUE;
  1156. }
  1157. }
  1158. Uint32 SDL_RegisterEvents(int numevents)
  1159. {
  1160. Uint32 event_base;
  1161. if ((numevents > 0) && (SDL_userevents + numevents <= SDL_EVENT_LAST)) {
  1162. event_base = SDL_userevents;
  1163. SDL_userevents += numevents;
  1164. } else {
  1165. event_base = (Uint32)-1;
  1166. }
  1167. return event_base;
  1168. }
  1169. int SDL_SendAppEvent(SDL_EventType eventType)
  1170. {
  1171. int posted;
  1172. posted = 0;
  1173. if (SDL_EventEnabled(eventType)) {
  1174. SDL_Event event;
  1175. event.type = eventType;
  1176. event.common.timestamp = 0;
  1177. posted = (SDL_PushEvent(&event) > 0);
  1178. }
  1179. return posted;
  1180. }
  1181. int SDL_SendSysWMEvent(SDL_SysWMmsg *message)
  1182. {
  1183. int posted;
  1184. posted = 0;
  1185. if (SDL_EventEnabled(SDL_EVENT_SYSWM)) {
  1186. SDL_Event event;
  1187. SDL_memset(&event, 0, sizeof(event));
  1188. event.type = SDL_EVENT_SYSWM;
  1189. event.common.timestamp = 0;
  1190. event.syswm.msg = message;
  1191. posted = (SDL_PushEvent(&event) > 0);
  1192. }
  1193. /* Update internal event state */
  1194. return posted;
  1195. }
  1196. int SDL_SendKeymapChangedEvent(void)
  1197. {
  1198. return SDL_SendAppEvent(SDL_EVENT_KEYMAP_CHANGED);
  1199. }
  1200. int SDL_SendLocaleChangedEvent(void)
  1201. {
  1202. return SDL_SendAppEvent(SDL_EVENT_LOCALE_CHANGED);
  1203. }
  1204. int SDL_SendSystemThemeChangedEvent(void)
  1205. {
  1206. return SDL_SendAppEvent(SDL_EVENT_SYSTEM_THEME_CHANGED);
  1207. }
  1208. int SDL_InitEvents(void)
  1209. {
  1210. #ifndef SDL_JOYSTICK_DISABLED
  1211. SDL_AddHintCallback(SDL_HINT_AUTO_UPDATE_JOYSTICKS, SDL_AutoUpdateJoysticksChanged, NULL);
  1212. #endif
  1213. #ifndef SDL_SENSOR_DISABLED
  1214. SDL_AddHintCallback(SDL_HINT_AUTO_UPDATE_SENSORS, SDL_AutoUpdateSensorsChanged, NULL);
  1215. #endif
  1216. SDL_AddHintCallback(SDL_HINT_EVENT_LOGGING, SDL_EventLoggingChanged, NULL);
  1217. SDL_AddHintCallback(SDL_HINT_POLL_SENTINEL, SDL_PollSentinelChanged, NULL);
  1218. if (SDL_StartEventLoop() < 0) {
  1219. SDL_DelHintCallback(SDL_HINT_EVENT_LOGGING, SDL_EventLoggingChanged, NULL);
  1220. return -1;
  1221. }
  1222. SDL_InitQuit();
  1223. return 0;
  1224. }
  1225. void SDL_QuitEvents(void)
  1226. {
  1227. SDL_QuitQuit();
  1228. SDL_StopEventLoop();
  1229. SDL_DelHintCallback(SDL_HINT_POLL_SENTINEL, SDL_PollSentinelChanged, NULL);
  1230. SDL_DelHintCallback(SDL_HINT_EVENT_LOGGING, SDL_EventLoggingChanged, NULL);
  1231. #ifndef SDL_JOYSTICK_DISABLED
  1232. SDL_DelHintCallback(SDL_HINT_AUTO_UPDATE_JOYSTICKS, SDL_AutoUpdateJoysticksChanged, NULL);
  1233. #endif
  1234. #ifndef SDL_SENSOR_DISABLED
  1235. SDL_DelHintCallback(SDL_HINT_AUTO_UPDATE_SENSORS, SDL_AutoUpdateSensorsChanged, NULL);
  1236. #endif
  1237. }