SDL_events.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  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 event handling code for SDL
  20. #include "SDL_events_c.h"
  21. #include "SDL_eventwatch_c.h"
  22. #include "SDL_windowevents_c.h"
  23. #include "../SDL_hints_c.h"
  24. #include "../audio/SDL_audio_c.h"
  25. #include "../camera/SDL_camera_c.h"
  26. #include "../timer/SDL_timer_c.h"
  27. #ifndef SDL_JOYSTICK_DISABLED
  28. #include "../joystick/SDL_joystick_c.h"
  29. #endif
  30. #ifndef SDL_SENSOR_DISABLED
  31. #include "../sensor/SDL_sensor_c.h"
  32. #endif
  33. #include "../video/SDL_sysvideo.h"
  34. #ifdef SDL_PLATFORM_ANDROID
  35. #include "../core/android/SDL_android.h"
  36. #include "../video/android/SDL_androidevents.h"
  37. #endif
  38. // An arbitrary limit so we don't have unbounded growth
  39. #define SDL_MAX_QUEUED_EVENTS 65535
  40. // Determines how often we pump events if joystick or sensor subsystems are active
  41. #define ENUMERATION_POLL_INTERVAL_NS (3 * SDL_NS_PER_SECOND)
  42. // Determines how often to pump events if joysticks or sensors are actively being read
  43. #define EVENT_POLL_INTERVAL_NS SDL_MS_TO_NS(1)
  44. // Make sure the type in the SDL_Event aligns properly across the union
  45. SDL_COMPILE_TIME_ASSERT(SDL_Event_type, sizeof(Uint32) == sizeof(SDL_EventType));
  46. #define SDL2_SYSWMEVENT 0x201
  47. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  48. #include "../core/windows/SDL_windows.h"
  49. #endif
  50. #ifdef SDL_VIDEO_DRIVER_X11
  51. #include <X11/Xlib.h>
  52. #endif
  53. typedef struct SDL2_version
  54. {
  55. Uint8 major;
  56. Uint8 minor;
  57. Uint8 patch;
  58. } SDL2_version;
  59. typedef enum
  60. {
  61. SDL2_SYSWM_UNKNOWN
  62. } SDL2_SYSWM_TYPE;
  63. typedef struct SDL2_SysWMmsg
  64. {
  65. SDL2_version version;
  66. SDL2_SYSWM_TYPE subsystem;
  67. union
  68. {
  69. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  70. struct {
  71. HWND hwnd; /**< The window for the message */
  72. UINT msg; /**< The type of message */
  73. WPARAM wParam; /**< WORD message parameter */
  74. LPARAM lParam; /**< LONG message parameter */
  75. } win;
  76. #endif
  77. #ifdef SDL_VIDEO_DRIVER_X11
  78. struct {
  79. XEvent event;
  80. } x11;
  81. #endif
  82. /* Can't have an empty union */
  83. int dummy;
  84. } msg;
  85. } SDL2_SysWMmsg;
  86. static SDL_EventWatchList SDL_event_watchers;
  87. static SDL_AtomicInt SDL_sentinel_pending;
  88. static Uint32 SDL_last_event_id = 0;
  89. typedef struct
  90. {
  91. Uint32 bits[8];
  92. } SDL_DisabledEventBlock;
  93. static SDL_DisabledEventBlock *SDL_disabled_events[256];
  94. static SDL_AtomicInt SDL_userevents;
  95. typedef struct SDL_TemporaryMemory
  96. {
  97. void *memory;
  98. struct SDL_TemporaryMemory *prev;
  99. struct SDL_TemporaryMemory *next;
  100. } SDL_TemporaryMemory;
  101. typedef struct SDL_TemporaryMemoryState
  102. {
  103. SDL_TemporaryMemory *head;
  104. SDL_TemporaryMemory *tail;
  105. } SDL_TemporaryMemoryState;
  106. static SDL_TLSID SDL_temporary_memory;
  107. typedef struct SDL_EventEntry
  108. {
  109. SDL_Event event;
  110. SDL_TemporaryMemory *memory;
  111. struct SDL_EventEntry *prev;
  112. struct SDL_EventEntry *next;
  113. } SDL_EventEntry;
  114. static struct
  115. {
  116. SDL_Mutex *lock;
  117. bool active;
  118. SDL_AtomicInt count;
  119. int max_events_seen;
  120. SDL_EventEntry *head;
  121. SDL_EventEntry *tail;
  122. SDL_EventEntry *free;
  123. } SDL_EventQ = { NULL, false, { 0 }, 0, NULL, NULL, NULL };
  124. static void SDL_CleanupTemporaryMemory(void *data)
  125. {
  126. SDL_TemporaryMemoryState *state = (SDL_TemporaryMemoryState *)data;
  127. SDL_FreeTemporaryMemory();
  128. SDL_free(state);
  129. }
  130. static SDL_TemporaryMemoryState *SDL_GetTemporaryMemoryState(bool create)
  131. {
  132. SDL_TemporaryMemoryState *state;
  133. state = (SDL_TemporaryMemoryState *)SDL_GetTLS(&SDL_temporary_memory);
  134. if (!state) {
  135. if (!create) {
  136. return NULL;
  137. }
  138. state = (SDL_TemporaryMemoryState *)SDL_calloc(1, sizeof(*state));
  139. if (!state) {
  140. return NULL;
  141. }
  142. if (!SDL_SetTLS(&SDL_temporary_memory, state, SDL_CleanupTemporaryMemory)) {
  143. SDL_free(state);
  144. return NULL;
  145. }
  146. }
  147. return state;
  148. }
  149. static SDL_TemporaryMemory *SDL_GetTemporaryMemoryEntry(SDL_TemporaryMemoryState *state, const void *mem)
  150. {
  151. SDL_TemporaryMemory *entry;
  152. // Start from the end, it's likely to have been recently allocated
  153. for (entry = state->tail; entry; entry = entry->prev) {
  154. if (mem == entry->memory) {
  155. return entry;
  156. }
  157. }
  158. return NULL;
  159. }
  160. static void SDL_LinkTemporaryMemoryEntry(SDL_TemporaryMemoryState *state, SDL_TemporaryMemory *entry)
  161. {
  162. entry->prev = state->tail;
  163. entry->next = NULL;
  164. if (state->tail) {
  165. state->tail->next = entry;
  166. } else {
  167. state->head = entry;
  168. }
  169. state->tail = entry;
  170. }
  171. static void SDL_UnlinkTemporaryMemoryEntry(SDL_TemporaryMemoryState *state, SDL_TemporaryMemory *entry)
  172. {
  173. if (state->head == entry) {
  174. state->head = entry->next;
  175. }
  176. if (state->tail == entry) {
  177. state->tail = entry->prev;
  178. }
  179. if (entry->prev) {
  180. entry->prev->next = entry->next;
  181. }
  182. if (entry->next) {
  183. entry->next->prev = entry->prev;
  184. }
  185. entry->prev = NULL;
  186. entry->next = NULL;
  187. }
  188. static void SDL_FreeTemporaryMemoryEntry(SDL_TemporaryMemoryState *state, SDL_TemporaryMemory *entry, bool free_data)
  189. {
  190. if (free_data) {
  191. SDL_free(entry->memory);
  192. }
  193. SDL_free(entry);
  194. }
  195. static void SDL_LinkTemporaryMemoryToEvent(SDL_EventEntry *event, const void *mem)
  196. {
  197. SDL_TemporaryMemoryState *state;
  198. SDL_TemporaryMemory *entry;
  199. state = SDL_GetTemporaryMemoryState(false);
  200. if (!state) {
  201. return;
  202. }
  203. entry = SDL_GetTemporaryMemoryEntry(state, mem);
  204. if (entry) {
  205. SDL_UnlinkTemporaryMemoryEntry(state, entry);
  206. entry->next = event->memory;
  207. event->memory = entry;
  208. }
  209. }
  210. static void SDL_TransferSysWMMemoryToEvent(SDL_EventEntry *event)
  211. {
  212. SDL2_SysWMmsg **wmmsg = (SDL2_SysWMmsg **)((&event->event.common)+1);
  213. SDL2_SysWMmsg *mem = SDL_AllocateTemporaryMemory(sizeof(*mem));
  214. if (mem) {
  215. SDL_copyp(mem, *wmmsg);
  216. *wmmsg = mem;
  217. SDL_LinkTemporaryMemoryToEvent(event, mem);
  218. }
  219. }
  220. // Transfer the event memory from the thread-local event memory list to the event
  221. static void SDL_TransferTemporaryMemoryToEvent(SDL_EventEntry *event)
  222. {
  223. switch (event->event.type) {
  224. case SDL_EVENT_TEXT_EDITING:
  225. SDL_LinkTemporaryMemoryToEvent(event, event->event.edit.text);
  226. break;
  227. case SDL_EVENT_TEXT_EDITING_CANDIDATES:
  228. SDL_LinkTemporaryMemoryToEvent(event, event->event.edit_candidates.candidates);
  229. break;
  230. case SDL_EVENT_TEXT_INPUT:
  231. SDL_LinkTemporaryMemoryToEvent(event, event->event.text.text);
  232. break;
  233. case SDL_EVENT_DROP_BEGIN:
  234. case SDL_EVENT_DROP_FILE:
  235. case SDL_EVENT_DROP_TEXT:
  236. case SDL_EVENT_DROP_COMPLETE:
  237. case SDL_EVENT_DROP_POSITION:
  238. SDL_LinkTemporaryMemoryToEvent(event, event->event.drop.source);
  239. SDL_LinkTemporaryMemoryToEvent(event, event->event.drop.data);
  240. break;
  241. case SDL_EVENT_CLIPBOARD_UPDATE:
  242. SDL_LinkTemporaryMemoryToEvent(event, event->event.clipboard.mime_types);
  243. break;
  244. case SDL2_SYSWMEVENT:
  245. // We need to copy the stack pointer into temporary memory
  246. SDL_TransferSysWMMemoryToEvent(event);
  247. break;
  248. default:
  249. break;
  250. }
  251. }
  252. // Transfer the event memory from the event to the thread-local event memory list
  253. static void SDL_TransferTemporaryMemoryFromEvent(SDL_EventEntry *event)
  254. {
  255. SDL_TemporaryMemoryState *state;
  256. SDL_TemporaryMemory *entry, *next;
  257. if (!event->memory) {
  258. return;
  259. }
  260. state = SDL_GetTemporaryMemoryState(true);
  261. if (!state) {
  262. return; // this is now a leak, but you probably have bigger problems if malloc failed.
  263. }
  264. for (entry = event->memory; entry; entry = next) {
  265. next = entry->next;
  266. SDL_LinkTemporaryMemoryEntry(state, entry);
  267. }
  268. event->memory = NULL;
  269. }
  270. static void *SDL_FreeLater(void *memory)
  271. {
  272. SDL_TemporaryMemoryState *state;
  273. if (memory == NULL) {
  274. return NULL;
  275. }
  276. // Make sure we're not adding this to the list twice
  277. //SDL_assert(!SDL_ClaimTemporaryMemory(memory));
  278. state = SDL_GetTemporaryMemoryState(true);
  279. if (!state) {
  280. return memory; // this is now a leak, but you probably have bigger problems if malloc failed.
  281. }
  282. SDL_TemporaryMemory *entry = (SDL_TemporaryMemory *)SDL_malloc(sizeof(*entry));
  283. if (!entry) {
  284. return memory; // this is now a leak, but you probably have bigger problems if malloc failed. We could probably pool up and reuse entries, though.
  285. }
  286. entry->memory = memory;
  287. SDL_LinkTemporaryMemoryEntry(state, entry);
  288. return memory;
  289. }
  290. void *SDL_AllocateTemporaryMemory(size_t size)
  291. {
  292. return SDL_FreeLater(SDL_malloc(size));
  293. }
  294. const char *SDL_CreateTemporaryString(const char *string)
  295. {
  296. if (string) {
  297. return (const char *)SDL_FreeLater(SDL_strdup(string));
  298. }
  299. return NULL;
  300. }
  301. void *SDL_ClaimTemporaryMemory(const void *mem)
  302. {
  303. SDL_TemporaryMemoryState *state;
  304. state = SDL_GetTemporaryMemoryState(false);
  305. if (state && mem) {
  306. SDL_TemporaryMemory *entry = SDL_GetTemporaryMemoryEntry(state, mem);
  307. if (entry) {
  308. SDL_UnlinkTemporaryMemoryEntry(state, entry);
  309. SDL_FreeTemporaryMemoryEntry(state, entry, false);
  310. return (void *)mem;
  311. }
  312. }
  313. return NULL;
  314. }
  315. void SDL_FreeTemporaryMemory(void)
  316. {
  317. SDL_TemporaryMemoryState *state;
  318. state = SDL_GetTemporaryMemoryState(false);
  319. if (!state) {
  320. return;
  321. }
  322. while (state->head) {
  323. SDL_TemporaryMemory *entry = state->head;
  324. SDL_UnlinkTemporaryMemoryEntry(state, entry);
  325. SDL_FreeTemporaryMemoryEntry(state, entry, true);
  326. }
  327. }
  328. #ifndef SDL_JOYSTICK_DISABLED
  329. static bool SDL_update_joysticks = true;
  330. static void SDLCALL SDL_AutoUpdateJoysticksChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  331. {
  332. SDL_update_joysticks = SDL_GetStringBoolean(hint, true);
  333. }
  334. #endif // !SDL_JOYSTICK_DISABLED
  335. #ifndef SDL_SENSOR_DISABLED
  336. static bool SDL_update_sensors = true;
  337. static void SDLCALL SDL_AutoUpdateSensorsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  338. {
  339. SDL_update_sensors = SDL_GetStringBoolean(hint, true);
  340. }
  341. #endif // !SDL_SENSOR_DISABLED
  342. static void SDLCALL SDL_PollSentinelChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  343. {
  344. SDL_SetEventEnabled(SDL_EVENT_POLL_SENTINEL, SDL_GetStringBoolean(hint, true));
  345. }
  346. /**
  347. * Verbosity of logged events as defined in SDL_HINT_EVENT_LOGGING:
  348. * - 0: (default) no logging
  349. * - 1: logging of most events
  350. * - 2: as above, plus mouse, pen, and finger motion
  351. */
  352. static int SDL_EventLoggingVerbosity = 0;
  353. static void SDLCALL SDL_EventLoggingChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  354. {
  355. SDL_EventLoggingVerbosity = (hint && *hint) ? SDL_clamp(SDL_atoi(hint), 0, 3) : 0;
  356. }
  357. static void SDL_LogEvent(const SDL_Event *event)
  358. {
  359. static const char *pen_axisnames[] = { "PRESSURE", "XTILT", "YTILT", "DISTANCE", "ROTATION", "SLIDER", "TANGENTIAL_PRESSURE" };
  360. SDL_COMPILE_TIME_ASSERT(pen_axisnames_array_matches, SDL_arraysize(pen_axisnames) == SDL_PEN_AXIS_COUNT);
  361. char name[64];
  362. char details[128];
  363. // sensor/mouse/pen/finger motion are spammy, ignore these if they aren't demanded.
  364. if ((SDL_EventLoggingVerbosity < 2) &&
  365. ((event->type == SDL_EVENT_MOUSE_MOTION) ||
  366. (event->type == SDL_EVENT_FINGER_MOTION) ||
  367. (event->type == SDL_EVENT_PEN_AXIS) ||
  368. (event->type == SDL_EVENT_PEN_MOTION) ||
  369. (event->type == SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION) ||
  370. (event->type == SDL_EVENT_GAMEPAD_SENSOR_UPDATE) ||
  371. (event->type == SDL_EVENT_SENSOR_UPDATE))) {
  372. return;
  373. }
  374. // this is to make (void)SDL_snprintf() calls cleaner.
  375. #define uint unsigned int
  376. name[0] = '\0';
  377. details[0] = '\0';
  378. // !!! FIXME: This code is kinda ugly, sorry.
  379. if ((event->type >= SDL_EVENT_USER) && (event->type <= SDL_EVENT_LAST)) {
  380. char plusstr[16];
  381. SDL_strlcpy(name, "SDL_EVENT_USER", sizeof(name));
  382. if (event->type > SDL_EVENT_USER) {
  383. (void)SDL_snprintf(plusstr, sizeof(plusstr), "+%u", ((uint)event->type) - SDL_EVENT_USER);
  384. } else {
  385. plusstr[0] = '\0';
  386. }
  387. (void)SDL_snprintf(details, sizeof(details), "%s (timestamp=%u windowid=%u code=%d data1=%p data2=%p)",
  388. plusstr, (uint)event->user.timestamp, (uint)event->user.windowID,
  389. (int)event->user.code, event->user.data1, event->user.data2);
  390. }
  391. switch (event->type) {
  392. #define SDL_EVENT_CASE(x) \
  393. case x: \
  394. SDL_strlcpy(name, #x, sizeof(name));
  395. SDL_EVENT_CASE(SDL_EVENT_FIRST)
  396. SDL_strlcpy(details, " (THIS IS PROBABLY A BUG!)", sizeof(details));
  397. break;
  398. SDL_EVENT_CASE(SDL_EVENT_QUIT)
  399. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u)", (uint)event->quit.timestamp);
  400. break;
  401. SDL_EVENT_CASE(SDL_EVENT_TERMINATING)
  402. break;
  403. SDL_EVENT_CASE(SDL_EVENT_LOW_MEMORY)
  404. break;
  405. SDL_EVENT_CASE(SDL_EVENT_WILL_ENTER_BACKGROUND)
  406. break;
  407. SDL_EVENT_CASE(SDL_EVENT_DID_ENTER_BACKGROUND)
  408. break;
  409. SDL_EVENT_CASE(SDL_EVENT_WILL_ENTER_FOREGROUND)
  410. break;
  411. SDL_EVENT_CASE(SDL_EVENT_DID_ENTER_FOREGROUND)
  412. break;
  413. SDL_EVENT_CASE(SDL_EVENT_LOCALE_CHANGED)
  414. break;
  415. SDL_EVENT_CASE(SDL_EVENT_SYSTEM_THEME_CHANGED)
  416. break;
  417. SDL_EVENT_CASE(SDL_EVENT_KEYMAP_CHANGED)
  418. break;
  419. SDL_EVENT_CASE(SDL_EVENT_CLIPBOARD_UPDATE)
  420. break;
  421. #define SDL_RENDEREVENT_CASE(x) \
  422. case x: \
  423. SDL_strlcpy(name, #x, sizeof(name)); \
  424. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u event=%s windowid=%u)", \
  425. (uint)event->display.timestamp, name, (uint)event->render.windowID); \
  426. break
  427. SDL_RENDEREVENT_CASE(SDL_EVENT_RENDER_TARGETS_RESET);
  428. SDL_RENDEREVENT_CASE(SDL_EVENT_RENDER_DEVICE_RESET);
  429. SDL_RENDEREVENT_CASE(SDL_EVENT_RENDER_DEVICE_LOST);
  430. #define SDL_DISPLAYEVENT_CASE(x) \
  431. case x: \
  432. SDL_strlcpy(name, #x, sizeof(name)); \
  433. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u display=%u event=%s data1=%d, data2=%d)", \
  434. (uint)event->display.timestamp, (uint)event->display.displayID, name, (int)event->display.data1, (int)event->display.data2); \
  435. break
  436. SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_ORIENTATION);
  437. SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_ADDED);
  438. SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_REMOVED);
  439. SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_MOVED);
  440. SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_DESKTOP_MODE_CHANGED);
  441. SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED);
  442. SDL_DISPLAYEVENT_CASE(SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED);
  443. #undef SDL_DISPLAYEVENT_CASE
  444. #define SDL_WINDOWEVENT_CASE(x) \
  445. case x: \
  446. SDL_strlcpy(name, #x, sizeof(name)); \
  447. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u event=%s data1=%d data2=%d)", \
  448. (uint)event->window.timestamp, (uint)event->window.windowID, name, (int)event->window.data1, (int)event->window.data2); \
  449. break
  450. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_SHOWN);
  451. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_HIDDEN);
  452. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_EXPOSED);
  453. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MOVED);
  454. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_RESIZED);
  455. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED);
  456. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_METAL_VIEW_RESIZED);
  457. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_SAFE_AREA_CHANGED);
  458. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MINIMIZED);
  459. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MAXIMIZED);
  460. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_RESTORED);
  461. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MOUSE_ENTER);
  462. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_MOUSE_LEAVE);
  463. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_FOCUS_GAINED);
  464. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_FOCUS_LOST);
  465. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_CLOSE_REQUESTED);
  466. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_HIT_TEST);
  467. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_ICCPROF_CHANGED);
  468. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_DISPLAY_CHANGED);
  469. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED);
  470. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_OCCLUDED);
  471. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_ENTER_FULLSCREEN);
  472. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_LEAVE_FULLSCREEN);
  473. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_DESTROYED);
  474. SDL_WINDOWEVENT_CASE(SDL_EVENT_WINDOW_HDR_STATE_CHANGED);
  475. #undef SDL_WINDOWEVENT_CASE
  476. #define PRINT_KEYDEV_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%u)", (uint)event->kdevice.timestamp, (uint)event->kdevice.which)
  477. SDL_EVENT_CASE(SDL_EVENT_KEYBOARD_ADDED)
  478. PRINT_KEYDEV_EVENT(event);
  479. break;
  480. SDL_EVENT_CASE(SDL_EVENT_KEYBOARD_REMOVED)
  481. PRINT_KEYDEV_EVENT(event);
  482. break;
  483. #undef PRINT_KEYDEV_EVENT
  484. #define PRINT_KEY_EVENT(event) \
  485. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u state=%s repeat=%s scancode=%u keycode=%u mod=0x%x)", \
  486. (uint)event->key.timestamp, (uint)event->key.windowID, (uint)event->key.which, \
  487. event->key.down ? "pressed" : "released", \
  488. event->key.repeat ? "true" : "false", \
  489. (uint)event->key.scancode, \
  490. (uint)event->key.key, \
  491. (uint)event->key.mod)
  492. SDL_EVENT_CASE(SDL_EVENT_KEY_DOWN)
  493. PRINT_KEY_EVENT(event);
  494. break;
  495. SDL_EVENT_CASE(SDL_EVENT_KEY_UP)
  496. PRINT_KEY_EVENT(event);
  497. break;
  498. #undef PRINT_KEY_EVENT
  499. SDL_EVENT_CASE(SDL_EVENT_TEXT_EDITING)
  500. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u text='%s' start=%d length=%d)",
  501. (uint)event->edit.timestamp, (uint)event->edit.windowID,
  502. event->edit.text, (int)event->edit.start, (int)event->edit.length);
  503. break;
  504. SDL_EVENT_CASE(SDL_EVENT_TEXT_EDITING_CANDIDATES)
  505. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u num_candidates=%d selected_candidate=%d)",
  506. (uint)event->edit_candidates.timestamp, (uint)event->edit_candidates.windowID,
  507. (int)event->edit_candidates.num_candidates, (int)event->edit_candidates.selected_candidate);
  508. break;
  509. SDL_EVENT_CASE(SDL_EVENT_TEXT_INPUT)
  510. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u text='%s')", (uint)event->text.timestamp, (uint)event->text.windowID, event->text.text);
  511. break;
  512. #define PRINT_MOUSEDEV_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%u)", (uint)event->mdevice.timestamp, (uint)event->mdevice.which)
  513. SDL_EVENT_CASE(SDL_EVENT_MOUSE_ADDED)
  514. PRINT_MOUSEDEV_EVENT(event);
  515. break;
  516. SDL_EVENT_CASE(SDL_EVENT_MOUSE_REMOVED)
  517. PRINT_MOUSEDEV_EVENT(event);
  518. break;
  519. #undef PRINT_MOUSEDEV_EVENT
  520. SDL_EVENT_CASE(SDL_EVENT_MOUSE_MOTION)
  521. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u state=%u x=%g y=%g xrel=%g yrel=%g)",
  522. (uint)event->motion.timestamp, (uint)event->motion.windowID,
  523. (uint)event->motion.which, (uint)event->motion.state,
  524. event->motion.x, event->motion.y,
  525. event->motion.xrel, event->motion.yrel);
  526. break;
  527. #define PRINT_MBUTTON_EVENT(event) \
  528. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u button=%u state=%s clicks=%u x=%g y=%g)", \
  529. (uint)event->button.timestamp, (uint)event->button.windowID, \
  530. (uint)event->button.which, (uint)event->button.button, \
  531. event->button.down ? "pressed" : "released", \
  532. (uint)event->button.clicks, event->button.x, event->button.y)
  533. SDL_EVENT_CASE(SDL_EVENT_MOUSE_BUTTON_DOWN)
  534. PRINT_MBUTTON_EVENT(event);
  535. break;
  536. SDL_EVENT_CASE(SDL_EVENT_MOUSE_BUTTON_UP)
  537. PRINT_MBUTTON_EVENT(event);
  538. break;
  539. #undef PRINT_MBUTTON_EVENT
  540. SDL_EVENT_CASE(SDL_EVENT_MOUSE_WHEEL)
  541. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u x=%g y=%g direction=%s)",
  542. (uint)event->wheel.timestamp, (uint)event->wheel.windowID,
  543. (uint)event->wheel.which, event->wheel.x, event->wheel.y,
  544. event->wheel.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped");
  545. break;
  546. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_AXIS_MOTION)
  547. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d axis=%u value=%d)",
  548. (uint)event->jaxis.timestamp, (int)event->jaxis.which,
  549. (uint)event->jaxis.axis, (int)event->jaxis.value);
  550. break;
  551. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_BALL_MOTION)
  552. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d ball=%u xrel=%d yrel=%d)",
  553. (uint)event->jball.timestamp, (int)event->jball.which,
  554. (uint)event->jball.ball, (int)event->jball.xrel, (int)event->jball.yrel);
  555. break;
  556. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_HAT_MOTION)
  557. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d hat=%u value=%u)",
  558. (uint)event->jhat.timestamp, (int)event->jhat.which,
  559. (uint)event->jhat.hat, (uint)event->jhat.value);
  560. break;
  561. #define PRINT_JBUTTON_EVENT(event) \
  562. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d button=%u state=%s)", \
  563. (uint)event->jbutton.timestamp, (int)event->jbutton.which, \
  564. (uint)event->jbutton.button, event->jbutton.down ? "pressed" : "released")
  565. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_BUTTON_DOWN)
  566. PRINT_JBUTTON_EVENT(event);
  567. break;
  568. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_BUTTON_UP)
  569. PRINT_JBUTTON_EVENT(event);
  570. break;
  571. #undef PRINT_JBUTTON_EVENT
  572. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_BATTERY_UPDATED)
  573. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d state=%u percent=%d)",
  574. (uint)event->jbattery.timestamp, (int)event->jbattery.which,
  575. event->jbattery.state, event->jbattery.percent);
  576. break;
  577. #define PRINT_JOYDEV_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d)", (uint)event->jdevice.timestamp, (int)event->jdevice.which)
  578. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_ADDED)
  579. PRINT_JOYDEV_EVENT(event);
  580. break;
  581. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_REMOVED)
  582. PRINT_JOYDEV_EVENT(event);
  583. break;
  584. SDL_EVENT_CASE(SDL_EVENT_JOYSTICK_UPDATE_COMPLETE)
  585. PRINT_JOYDEV_EVENT(event);
  586. break;
  587. #undef PRINT_JOYDEV_EVENT
  588. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_AXIS_MOTION)
  589. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d axis=%u value=%d)",
  590. (uint)event->gaxis.timestamp, (int)event->gaxis.which,
  591. (uint)event->gaxis.axis, (int)event->gaxis.value);
  592. break;
  593. #define PRINT_CBUTTON_EVENT(event) \
  594. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d button=%u state=%s)", \
  595. (uint)event->gbutton.timestamp, (int)event->gbutton.which, \
  596. (uint)event->gbutton.button, event->gbutton.down ? "pressed" : "released")
  597. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_BUTTON_DOWN)
  598. PRINT_CBUTTON_EVENT(event);
  599. break;
  600. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_BUTTON_UP)
  601. PRINT_CBUTTON_EVENT(event);
  602. break;
  603. #undef PRINT_CBUTTON_EVENT
  604. #define PRINT_GAMEPADDEV_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d)", (uint)event->gdevice.timestamp, (int)event->gdevice.which)
  605. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_ADDED)
  606. PRINT_GAMEPADDEV_EVENT(event);
  607. break;
  608. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_REMOVED)
  609. PRINT_GAMEPADDEV_EVENT(event);
  610. break;
  611. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_REMAPPED)
  612. PRINT_GAMEPADDEV_EVENT(event);
  613. break;
  614. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_UPDATE_COMPLETE)
  615. PRINT_GAMEPADDEV_EVENT(event);
  616. break;
  617. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_STEAM_HANDLE_UPDATED)
  618. PRINT_GAMEPADDEV_EVENT(event);
  619. break;
  620. #undef PRINT_GAMEPADDEV_EVENT
  621. #define PRINT_CTOUCHPAD_EVENT(event) \
  622. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d touchpad=%d finger=%d x=%f y=%f pressure=%f)", \
  623. (uint)event->gtouchpad.timestamp, (int)event->gtouchpad.which, \
  624. (int)event->gtouchpad.touchpad, (int)event->gtouchpad.finger, \
  625. event->gtouchpad.x, event->gtouchpad.y, event->gtouchpad.pressure)
  626. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN)
  627. PRINT_CTOUCHPAD_EVENT(event);
  628. break;
  629. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_TOUCHPAD_UP)
  630. PRINT_CTOUCHPAD_EVENT(event);
  631. break;
  632. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION)
  633. PRINT_CTOUCHPAD_EVENT(event);
  634. break;
  635. #undef PRINT_CTOUCHPAD_EVENT
  636. SDL_EVENT_CASE(SDL_EVENT_GAMEPAD_SENSOR_UPDATE)
  637. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%d sensor=%d data[0]=%f data[1]=%f data[2]=%f)",
  638. (uint)event->gsensor.timestamp, (int)event->gsensor.which, (int)event->gsensor.sensor,
  639. event->gsensor.data[0], event->gsensor.data[1], event->gsensor.data[2]);
  640. break;
  641. #define PRINT_FINGER_EVENT(event) \
  642. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u touchid=%" SDL_PRIu64 " fingerid=%" SDL_PRIu64 " x=%f y=%f dx=%f dy=%f pressure=%f)", \
  643. (uint)event->tfinger.timestamp, event->tfinger.touchID, \
  644. event->tfinger.fingerID, event->tfinger.x, event->tfinger.y, \
  645. event->tfinger.dx, event->tfinger.dy, event->tfinger.pressure)
  646. SDL_EVENT_CASE(SDL_EVENT_FINGER_DOWN)
  647. PRINT_FINGER_EVENT(event);
  648. break;
  649. SDL_EVENT_CASE(SDL_EVENT_FINGER_UP)
  650. PRINT_FINGER_EVENT(event);
  651. break;
  652. SDL_EVENT_CASE(SDL_EVENT_FINGER_CANCELED)
  653. PRINT_FINGER_EVENT(event);
  654. break;
  655. SDL_EVENT_CASE(SDL_EVENT_FINGER_MOTION)
  656. PRINT_FINGER_EVENT(event);
  657. break;
  658. #undef PRINT_FINGER_EVENT
  659. #define PRINT_PTOUCH_EVENT(event) \
  660. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u pen_state=%u x=%g y=%g eraser=%s state=%s)", \
  661. (uint)event->ptouch.timestamp, (uint)event->ptouch.windowID, (uint)event->ptouch.which, (uint)event->ptouch.pen_state, event->ptouch.x, event->ptouch.y, \
  662. event->ptouch.eraser ? "yes" : "no", event->ptouch.down ? "down" : "up");
  663. SDL_EVENT_CASE(SDL_EVENT_PEN_DOWN)
  664. PRINT_PTOUCH_EVENT(event);
  665. break;
  666. SDL_EVENT_CASE(SDL_EVENT_PEN_UP)
  667. PRINT_PTOUCH_EVENT(event);
  668. break;
  669. #undef PRINT_PTOUCH_EVENT
  670. #define PRINT_PPROXIMITY_EVENT(event) \
  671. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u)", \
  672. (uint)event->pproximity.timestamp, (uint)event->pproximity.windowID, (uint)event->pproximity.which);
  673. SDL_EVENT_CASE(SDL_EVENT_PEN_PROXIMITY_IN)
  674. PRINT_PPROXIMITY_EVENT(event);
  675. break;
  676. SDL_EVENT_CASE(SDL_EVENT_PEN_PROXIMITY_OUT)
  677. PRINT_PPROXIMITY_EVENT(event);
  678. break;
  679. #undef PRINT_PPROXIMITY_EVENT
  680. SDL_EVENT_CASE(SDL_EVENT_PEN_AXIS)
  681. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u pen_state=%u x=%g y=%g axis=%s value=%g)",
  682. (uint)event->paxis.timestamp, (uint)event->paxis.windowID, (uint)event->paxis.which, (uint)event->paxis.pen_state, event->paxis.x, event->paxis.y,
  683. ((((int) event->paxis.axis) >= 0) && (event->paxis.axis < SDL_arraysize(pen_axisnames))) ? pen_axisnames[event->paxis.axis] : "[UNKNOWN]", event->paxis.value);
  684. break;
  685. SDL_EVENT_CASE(SDL_EVENT_PEN_MOTION)
  686. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u pen_state=%u x=%g y=%g)",
  687. (uint)event->pmotion.timestamp, (uint)event->pmotion.windowID, (uint)event->pmotion.which, (uint)event->pmotion.pen_state, event->pmotion.x, event->pmotion.y);
  688. break;
  689. #define PRINT_PBUTTON_EVENT(event) \
  690. (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u pen_state=%u x=%g y=%g button=%u state=%s)", \
  691. (uint)event->pbutton.timestamp, (uint)event->pbutton.windowID, (uint)event->pbutton.which, (uint)event->pbutton.pen_state, event->pbutton.x, event->pbutton.y, \
  692. (uint)event->pbutton.button, event->pbutton.down ? "down" : "up");
  693. SDL_EVENT_CASE(SDL_EVENT_PEN_BUTTON_DOWN)
  694. PRINT_PBUTTON_EVENT(event);
  695. break;
  696. SDL_EVENT_CASE(SDL_EVENT_PEN_BUTTON_UP)
  697. PRINT_PBUTTON_EVENT(event);
  698. break;
  699. #undef PRINT_PBUTTON_EVENT
  700. #define PRINT_DROP_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (data='%s' timestamp=%u windowid=%u x=%f y=%f)", event->drop.data, (uint)event->drop.timestamp, (uint)event->drop.windowID, event->drop.x, event->drop.y)
  701. SDL_EVENT_CASE(SDL_EVENT_DROP_FILE)
  702. PRINT_DROP_EVENT(event);
  703. break;
  704. SDL_EVENT_CASE(SDL_EVENT_DROP_TEXT)
  705. PRINT_DROP_EVENT(event);
  706. break;
  707. SDL_EVENT_CASE(SDL_EVENT_DROP_BEGIN)
  708. PRINT_DROP_EVENT(event);
  709. break;
  710. SDL_EVENT_CASE(SDL_EVENT_DROP_COMPLETE)
  711. PRINT_DROP_EVENT(event);
  712. break;
  713. SDL_EVENT_CASE(SDL_EVENT_DROP_POSITION)
  714. PRINT_DROP_EVENT(event);
  715. break;
  716. #undef PRINT_DROP_EVENT
  717. #define PRINT_AUDIODEV_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%u recording=%s)", (uint)event->adevice.timestamp, (uint)event->adevice.which, event->adevice.recording ? "true" : "false")
  718. SDL_EVENT_CASE(SDL_EVENT_AUDIO_DEVICE_ADDED)
  719. PRINT_AUDIODEV_EVENT(event);
  720. break;
  721. SDL_EVENT_CASE(SDL_EVENT_AUDIO_DEVICE_REMOVED)
  722. PRINT_AUDIODEV_EVENT(event);
  723. break;
  724. SDL_EVENT_CASE(SDL_EVENT_AUDIO_DEVICE_FORMAT_CHANGED)
  725. PRINT_AUDIODEV_EVENT(event);
  726. break;
  727. #undef PRINT_AUDIODEV_EVENT
  728. #define PRINT_CAMERADEV_EVENT(event) (void)SDL_snprintf(details, sizeof(details), " (timestamp=%u which=%u)", (uint)event->cdevice.timestamp, (uint)event->cdevice.which)
  729. SDL_EVENT_CASE(SDL_EVENT_CAMERA_DEVICE_ADDED)
  730. PRINT_CAMERADEV_EVENT(event);
  731. break;
  732. SDL_EVENT_CASE(SDL_EVENT_CAMERA_DEVICE_REMOVED)
  733. PRINT_CAMERADEV_EVENT(event);
  734. break;
  735. SDL_EVENT_CASE(SDL_EVENT_CAMERA_DEVICE_APPROVED)
  736. PRINT_CAMERADEV_EVENT(event);
  737. break;
  738. SDL_EVENT_CASE(SDL_EVENT_CAMERA_DEVICE_DENIED)
  739. PRINT_CAMERADEV_EVENT(event);
  740. break;
  741. #undef PRINT_CAMERADEV_EVENT
  742. SDL_EVENT_CASE(SDL_EVENT_SENSOR_UPDATE)
  743. (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)",
  744. (uint)event->sensor.timestamp, (int)event->sensor.which,
  745. event->sensor.data[0], event->sensor.data[1], event->sensor.data[2],
  746. event->sensor.data[3], event->sensor.data[4], event->sensor.data[5]);
  747. break;
  748. #undef SDL_EVENT_CASE
  749. case SDL_EVENT_POLL_SENTINEL:
  750. // No logging necessary for this one
  751. break;
  752. default:
  753. if (!name[0]) {
  754. if (event->type >= SDL_EVENT_USER) {
  755. SDL_strlcpy(name, "USER", sizeof(name));
  756. } else {
  757. SDL_strlcpy(name, "UNKNOWN", sizeof(name));
  758. }
  759. (void)SDL_snprintf(details, sizeof(details), " 0x%x", (uint)event->type);
  760. }
  761. break;
  762. }
  763. if (name[0]) {
  764. SDL_Log("SDL EVENT: %s%s", name, details);
  765. }
  766. #undef uint
  767. }
  768. void SDL_StopEventLoop(void)
  769. {
  770. const char *report = SDL_GetHint("SDL_EVENT_QUEUE_STATISTICS");
  771. int i;
  772. SDL_EventEntry *entry;
  773. SDL_LockMutex(SDL_EventQ.lock);
  774. SDL_EventQ.active = false;
  775. if (report && SDL_atoi(report)) {
  776. SDL_Log("SDL EVENT QUEUE: Maximum events in-flight: %d",
  777. SDL_EventQ.max_events_seen);
  778. }
  779. // Clean out EventQ
  780. for (entry = SDL_EventQ.head; entry;) {
  781. SDL_EventEntry *next = entry->next;
  782. SDL_TransferTemporaryMemoryFromEvent(entry);
  783. SDL_free(entry);
  784. entry = next;
  785. }
  786. for (entry = SDL_EventQ.free; entry;) {
  787. SDL_EventEntry *next = entry->next;
  788. SDL_free(entry);
  789. entry = next;
  790. }
  791. SDL_SetAtomicInt(&SDL_EventQ.count, 0);
  792. SDL_EventQ.max_events_seen = 0;
  793. SDL_EventQ.head = NULL;
  794. SDL_EventQ.tail = NULL;
  795. SDL_EventQ.free = NULL;
  796. SDL_SetAtomicInt(&SDL_sentinel_pending, 0);
  797. // Clear disabled event state
  798. for (i = 0; i < SDL_arraysize(SDL_disabled_events); ++i) {
  799. SDL_free(SDL_disabled_events[i]);
  800. SDL_disabled_events[i] = NULL;
  801. }
  802. SDL_QuitEventWatchList(&SDL_event_watchers);
  803. SDL_QuitWindowEventWatch();
  804. SDL_Mutex *lock = NULL;
  805. if (SDL_EventQ.lock) {
  806. lock = SDL_EventQ.lock;
  807. SDL_EventQ.lock = NULL;
  808. }
  809. SDL_UnlockMutex(lock);
  810. if (lock) {
  811. SDL_DestroyMutex(lock);
  812. }
  813. }
  814. // This function (and associated calls) may be called more than once
  815. bool SDL_StartEventLoop(void)
  816. {
  817. /* We'll leave the event queue alone, since we might have gotten
  818. some important events at launch (like SDL_EVENT_DROP_FILE)
  819. FIXME: Does this introduce any other bugs with events at startup?
  820. */
  821. // Create the lock and set ourselves active
  822. #ifndef SDL_THREADS_DISABLED
  823. if (!SDL_EventQ.lock) {
  824. SDL_EventQ.lock = SDL_CreateMutex();
  825. if (SDL_EventQ.lock == NULL) {
  826. return false;
  827. }
  828. }
  829. SDL_LockMutex(SDL_EventQ.lock);
  830. if (!SDL_InitEventWatchList(&SDL_event_watchers)) {
  831. SDL_UnlockMutex(SDL_EventQ.lock);
  832. return false;
  833. }
  834. #endif // !SDL_THREADS_DISABLED
  835. SDL_InitWindowEventWatch();
  836. SDL_EventQ.active = true;
  837. #ifndef SDL_THREADS_DISABLED
  838. SDL_UnlockMutex(SDL_EventQ.lock);
  839. #endif
  840. return true;
  841. }
  842. // Add an event to the event queue -- called with the queue locked
  843. static int SDL_AddEvent(SDL_Event *event)
  844. {
  845. SDL_EventEntry *entry;
  846. const int initial_count = SDL_GetAtomicInt(&SDL_EventQ.count);
  847. int final_count;
  848. if (initial_count >= SDL_MAX_QUEUED_EVENTS) {
  849. SDL_SetError("Event queue is full (%d events)", initial_count);
  850. return 0;
  851. }
  852. if (SDL_EventQ.free == NULL) {
  853. entry = (SDL_EventEntry *)SDL_malloc(sizeof(*entry));
  854. if (entry == NULL) {
  855. return 0;
  856. }
  857. } else {
  858. entry = SDL_EventQ.free;
  859. SDL_EventQ.free = entry->next;
  860. }
  861. if (SDL_EventLoggingVerbosity > 0) {
  862. SDL_LogEvent(event);
  863. }
  864. SDL_copyp(&entry->event, event);
  865. if (event->type == SDL_EVENT_POLL_SENTINEL) {
  866. SDL_AddAtomicInt(&SDL_sentinel_pending, 1);
  867. }
  868. entry->memory = NULL;
  869. SDL_TransferTemporaryMemoryToEvent(entry);
  870. if (SDL_EventQ.tail) {
  871. SDL_EventQ.tail->next = entry;
  872. entry->prev = SDL_EventQ.tail;
  873. SDL_EventQ.tail = entry;
  874. entry->next = NULL;
  875. } else {
  876. SDL_assert(!SDL_EventQ.head);
  877. SDL_EventQ.head = entry;
  878. SDL_EventQ.tail = entry;
  879. entry->prev = NULL;
  880. entry->next = NULL;
  881. }
  882. final_count = SDL_AddAtomicInt(&SDL_EventQ.count, 1) + 1;
  883. if (final_count > SDL_EventQ.max_events_seen) {
  884. SDL_EventQ.max_events_seen = final_count;
  885. }
  886. ++SDL_last_event_id;
  887. return 1;
  888. }
  889. // Remove an event from the queue -- called with the queue locked
  890. static void SDL_CutEvent(SDL_EventEntry *entry)
  891. {
  892. SDL_TransferTemporaryMemoryFromEvent(entry);
  893. if (entry->prev) {
  894. entry->prev->next = entry->next;
  895. }
  896. if (entry->next) {
  897. entry->next->prev = entry->prev;
  898. }
  899. if (entry == SDL_EventQ.head) {
  900. SDL_assert(entry->prev == NULL);
  901. SDL_EventQ.head = entry->next;
  902. }
  903. if (entry == SDL_EventQ.tail) {
  904. SDL_assert(entry->next == NULL);
  905. SDL_EventQ.tail = entry->prev;
  906. }
  907. if (entry->event.type == SDL_EVENT_POLL_SENTINEL) {
  908. SDL_AddAtomicInt(&SDL_sentinel_pending, -1);
  909. }
  910. entry->next = SDL_EventQ.free;
  911. SDL_EventQ.free = entry;
  912. SDL_assert(SDL_GetAtomicInt(&SDL_EventQ.count) > 0);
  913. SDL_AddAtomicInt(&SDL_EventQ.count, -1);
  914. }
  915. static void SDL_SendWakeupEvent(void)
  916. {
  917. #ifdef SDL_PLATFORM_ANDROID
  918. Android_SendLifecycleEvent(SDL_ANDROID_LIFECYCLE_WAKE);
  919. #else
  920. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  921. if (_this == NULL || !_this->SendWakeupEvent) {
  922. return;
  923. }
  924. SDL_LockMutex(_this->wakeup_lock);
  925. {
  926. if (_this->wakeup_window) {
  927. _this->SendWakeupEvent(_this, _this->wakeup_window);
  928. // No more wakeup events needed until we enter a new wait
  929. _this->wakeup_window = NULL;
  930. }
  931. }
  932. SDL_UnlockMutex(_this->wakeup_lock);
  933. #endif
  934. }
  935. // Lock the event queue, take a peep at it, and unlock it
  936. static int SDL_PeepEventsInternal(SDL_Event *events, int numevents, SDL_EventAction action,
  937. Uint32 minType, Uint32 maxType, bool include_sentinel)
  938. {
  939. int i, used, sentinels_expected = 0;
  940. // Lock the event queue
  941. used = 0;
  942. SDL_LockMutex(SDL_EventQ.lock);
  943. {
  944. // Don't look after we've quit
  945. if (!SDL_EventQ.active) {
  946. // We get a few spurious events at shutdown, so don't warn then
  947. if (action == SDL_GETEVENT) {
  948. SDL_SetError("The event system has been shut down");
  949. }
  950. SDL_UnlockMutex(SDL_EventQ.lock);
  951. return -1;
  952. }
  953. if (action == SDL_ADDEVENT) {
  954. if (!events) {
  955. SDL_UnlockMutex(SDL_EventQ.lock);
  956. return SDL_InvalidParamError("events");
  957. }
  958. for (i = 0; i < numevents; ++i) {
  959. used += SDL_AddEvent(&events[i]);
  960. }
  961. } else {
  962. SDL_EventEntry *entry, *next;
  963. Uint32 type;
  964. for (entry = SDL_EventQ.head; entry && (events == NULL || used < numevents); entry = next) {
  965. next = entry->next;
  966. type = entry->event.type;
  967. if (minType <= type && type <= maxType) {
  968. if (events) {
  969. SDL_copyp(&events[used], &entry->event);
  970. if (action == SDL_GETEVENT) {
  971. SDL_CutEvent(entry);
  972. }
  973. }
  974. if (type == SDL_EVENT_POLL_SENTINEL) {
  975. // Special handling for the sentinel event
  976. if (!include_sentinel) {
  977. // Skip it, we don't want to include it
  978. continue;
  979. }
  980. if (events == NULL || action != SDL_GETEVENT) {
  981. ++sentinels_expected;
  982. }
  983. if (SDL_GetAtomicInt(&SDL_sentinel_pending) > sentinels_expected) {
  984. // Skip it, there's another one pending
  985. continue;
  986. }
  987. }
  988. ++used;
  989. }
  990. }
  991. }
  992. }
  993. SDL_UnlockMutex(SDL_EventQ.lock);
  994. if (used > 0 && action == SDL_ADDEVENT) {
  995. SDL_SendWakeupEvent();
  996. }
  997. return used;
  998. }
  999. int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_EventAction action,
  1000. Uint32 minType, Uint32 maxType)
  1001. {
  1002. return SDL_PeepEventsInternal(events, numevents, action, minType, maxType, false);
  1003. }
  1004. bool SDL_HasEvent(Uint32 type)
  1005. {
  1006. return SDL_HasEvents(type, type);
  1007. }
  1008. bool SDL_HasEvents(Uint32 minType, Uint32 maxType)
  1009. {
  1010. bool found = false;
  1011. SDL_LockMutex(SDL_EventQ.lock);
  1012. {
  1013. if (SDL_EventQ.active) {
  1014. for (SDL_EventEntry *entry = SDL_EventQ.head; entry; entry = entry->next) {
  1015. const Uint32 type = entry->event.type;
  1016. if (minType <= type && type <= maxType) {
  1017. found = true;
  1018. break;
  1019. }
  1020. }
  1021. }
  1022. }
  1023. SDL_UnlockMutex(SDL_EventQ.lock);
  1024. return found;
  1025. }
  1026. void SDL_FlushEvent(Uint32 type)
  1027. {
  1028. SDL_FlushEvents(type, type);
  1029. }
  1030. void SDL_FlushEvents(Uint32 minType, Uint32 maxType)
  1031. {
  1032. SDL_EventEntry *entry, *next;
  1033. Uint32 type;
  1034. // Make sure the events are current
  1035. #if 0
  1036. /* Actually, we can't do this since we might be flushing while processing
  1037. a resize event, and calling this might trigger further resize events.
  1038. */
  1039. SDL_PumpEvents();
  1040. #endif
  1041. // Lock the event queue
  1042. SDL_LockMutex(SDL_EventQ.lock);
  1043. {
  1044. // Don't look after we've quit
  1045. if (!SDL_EventQ.active) {
  1046. SDL_UnlockMutex(SDL_EventQ.lock);
  1047. return;
  1048. }
  1049. for (entry = SDL_EventQ.head; entry; entry = next) {
  1050. next = entry->next;
  1051. type = entry->event.type;
  1052. if (minType <= type && type <= maxType) {
  1053. SDL_CutEvent(entry);
  1054. }
  1055. }
  1056. }
  1057. SDL_UnlockMutex(SDL_EventQ.lock);
  1058. }
  1059. typedef enum
  1060. {
  1061. SDL_MAIN_CALLBACK_WAITING,
  1062. SDL_MAIN_CALLBACK_COMPLETE,
  1063. SDL_MAIN_CALLBACK_CANCELED,
  1064. } SDL_MainThreadCallbackState;
  1065. typedef struct SDL_MainThreadCallbackEntry
  1066. {
  1067. SDL_MainThreadCallback callback;
  1068. void *userdata;
  1069. SDL_AtomicInt state;
  1070. SDL_Semaphore *semaphore;
  1071. struct SDL_MainThreadCallbackEntry *next;
  1072. } SDL_MainThreadCallbackEntry;
  1073. static SDL_Mutex *SDL_main_callbacks_lock;
  1074. static SDL_MainThreadCallbackEntry *SDL_main_callbacks_head;
  1075. static SDL_MainThreadCallbackEntry *SDL_main_callbacks_tail;
  1076. static SDL_MainThreadCallbackEntry *SDL_CreateMainThreadCallback(SDL_MainThreadCallback callback, void *userdata, bool wait_complete)
  1077. {
  1078. SDL_MainThreadCallbackEntry *entry = (SDL_MainThreadCallbackEntry *)SDL_malloc(sizeof(*entry));
  1079. if (!entry) {
  1080. return NULL;
  1081. }
  1082. entry->callback = callback;
  1083. entry->userdata = userdata;
  1084. SDL_SetAtomicInt(&entry->state, SDL_MAIN_CALLBACK_WAITING);
  1085. if (wait_complete) {
  1086. entry->semaphore = SDL_CreateSemaphore(0);
  1087. if (!entry->semaphore) {
  1088. SDL_free(entry);
  1089. return NULL;
  1090. }
  1091. } else {
  1092. entry->semaphore = NULL;
  1093. }
  1094. entry->next = NULL;
  1095. return entry;
  1096. }
  1097. static void SDL_DestroyMainThreadCallback(SDL_MainThreadCallbackEntry *entry)
  1098. {
  1099. if (entry->semaphore) {
  1100. SDL_DestroySemaphore(entry->semaphore);
  1101. }
  1102. SDL_free(entry);
  1103. }
  1104. static void SDL_InitMainThreadCallbacks(void)
  1105. {
  1106. SDL_main_callbacks_lock = SDL_CreateMutex();
  1107. SDL_assert(SDL_main_callbacks_head == NULL &&
  1108. SDL_main_callbacks_tail == NULL);
  1109. }
  1110. static void SDL_QuitMainThreadCallbacks(void)
  1111. {
  1112. SDL_MainThreadCallbackEntry *entry;
  1113. SDL_LockMutex(SDL_main_callbacks_lock);
  1114. {
  1115. entry = SDL_main_callbacks_head;
  1116. SDL_main_callbacks_head = NULL;
  1117. SDL_main_callbacks_tail = NULL;
  1118. }
  1119. SDL_UnlockMutex(SDL_main_callbacks_lock);
  1120. while (entry) {
  1121. SDL_MainThreadCallbackEntry *next = entry->next;
  1122. if (entry->semaphore) {
  1123. // Let the waiting thread know this is canceled
  1124. SDL_SetAtomicInt(&entry->state, SDL_MAIN_CALLBACK_CANCELED);
  1125. SDL_SignalSemaphore(entry->semaphore);
  1126. } else {
  1127. // Nobody's waiting for this, clean it up
  1128. SDL_DestroyMainThreadCallback(entry);
  1129. }
  1130. entry = next;
  1131. }
  1132. SDL_DestroyMutex(SDL_main_callbacks_lock);
  1133. SDL_main_callbacks_lock = NULL;
  1134. }
  1135. static void SDL_RunMainThreadCallbacks(void)
  1136. {
  1137. SDL_MainThreadCallbackEntry *entry;
  1138. SDL_LockMutex(SDL_main_callbacks_lock);
  1139. {
  1140. entry = SDL_main_callbacks_head;
  1141. SDL_main_callbacks_head = NULL;
  1142. SDL_main_callbacks_tail = NULL;
  1143. }
  1144. SDL_UnlockMutex(SDL_main_callbacks_lock);
  1145. while (entry) {
  1146. SDL_MainThreadCallbackEntry *next = entry->next;
  1147. entry->callback(entry->userdata);
  1148. if (entry->semaphore) {
  1149. // Let the waiting thread know this is done
  1150. SDL_SetAtomicInt(&entry->state, SDL_MAIN_CALLBACK_COMPLETE);
  1151. SDL_SignalSemaphore(entry->semaphore);
  1152. } else {
  1153. // Nobody's waiting for this, clean it up
  1154. SDL_DestroyMainThreadCallback(entry);
  1155. }
  1156. entry = next;
  1157. }
  1158. }
  1159. bool SDL_RunOnMainThread(SDL_MainThreadCallback callback, void *userdata, bool wait_complete)
  1160. {
  1161. if (SDL_IsMainThread() || !SDL_WasInit(SDL_INIT_EVENTS)) {
  1162. // No need to queue the callback
  1163. callback(userdata);
  1164. return true;
  1165. }
  1166. SDL_MainThreadCallbackEntry *entry = SDL_CreateMainThreadCallback(callback, userdata, wait_complete);
  1167. if (!entry) {
  1168. return false;
  1169. }
  1170. SDL_LockMutex(SDL_main_callbacks_lock);
  1171. {
  1172. if (SDL_main_callbacks_tail) {
  1173. SDL_main_callbacks_tail->next = entry;
  1174. SDL_main_callbacks_tail = entry;
  1175. } else {
  1176. SDL_main_callbacks_head = entry;
  1177. SDL_main_callbacks_tail = entry;
  1178. }
  1179. }
  1180. SDL_UnlockMutex(SDL_main_callbacks_lock);
  1181. // If the main thread is waiting for events, wake it up
  1182. SDL_SendWakeupEvent();
  1183. if (!wait_complete) {
  1184. // Queued for execution, wait not requested
  1185. return true;
  1186. }
  1187. // Maximum wait of 30 seconds to prevent deadlocking forever
  1188. const Sint32 MAX_CALLBACK_WAIT = 30 * 1000;
  1189. SDL_WaitSemaphoreTimeout(entry->semaphore, MAX_CALLBACK_WAIT);
  1190. switch (SDL_GetAtomicInt(&entry->state)) {
  1191. case SDL_MAIN_CALLBACK_COMPLETE:
  1192. // Execution complete!
  1193. SDL_DestroyMainThreadCallback(entry);
  1194. return true;
  1195. case SDL_MAIN_CALLBACK_CANCELED:
  1196. // The callback was canceled on the main thread
  1197. SDL_DestroyMainThreadCallback(entry);
  1198. return SDL_SetError("Callback canceled");
  1199. default:
  1200. // Probably hit a deadlock in the callback
  1201. // We can't destroy the entry as the semaphore will be signaled
  1202. // if it ever comes back, just leak it here.
  1203. return SDL_SetError("Callback timed out");
  1204. }
  1205. }
  1206. void SDL_PumpEventMaintenance(void)
  1207. {
  1208. #ifndef SDL_AUDIO_DISABLED
  1209. SDL_UpdateAudio();
  1210. #endif
  1211. #ifndef SDL_CAMERA_DISABLED
  1212. SDL_UpdateCamera();
  1213. #endif
  1214. #ifndef SDL_SENSOR_DISABLED
  1215. // Check for sensor state change
  1216. if (SDL_update_sensors) {
  1217. SDL_UpdateSensors();
  1218. }
  1219. #endif
  1220. #ifndef SDL_JOYSTICK_DISABLED
  1221. // Check for joystick state change
  1222. if (SDL_update_joysticks) {
  1223. SDL_UpdateJoysticks();
  1224. }
  1225. #endif
  1226. SDL_UpdateTrays();
  1227. SDL_SendPendingSignalEvents(); // in case we had a signal handler fire, etc.
  1228. }
  1229. // Run the system dependent event loops
  1230. static void SDL_PumpEventsInternal(bool push_sentinel)
  1231. {
  1232. // Free any temporary memory from old events
  1233. SDL_FreeTemporaryMemory();
  1234. // Release any keys held down from last frame
  1235. SDL_ReleaseAutoReleaseKeys();
  1236. // Run any pending main thread callbacks
  1237. SDL_RunMainThreadCallbacks();
  1238. #ifdef SDL_PLATFORM_ANDROID
  1239. // Android event processing is independent of the video subsystem
  1240. Android_PumpEvents(0);
  1241. #else
  1242. // Get events from the video subsystem
  1243. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  1244. if (_this) {
  1245. _this->PumpEvents(_this);
  1246. }
  1247. #endif
  1248. SDL_PumpEventMaintenance();
  1249. if (push_sentinel && SDL_EventEnabled(SDL_EVENT_POLL_SENTINEL)) {
  1250. SDL_Event sentinel;
  1251. // Make sure we don't already have a sentinel in the queue, and add one to the end
  1252. if (SDL_GetAtomicInt(&SDL_sentinel_pending) > 0) {
  1253. SDL_PeepEventsInternal(&sentinel, 1, SDL_GETEVENT, SDL_EVENT_POLL_SENTINEL, SDL_EVENT_POLL_SENTINEL, true);
  1254. }
  1255. sentinel.type = SDL_EVENT_POLL_SENTINEL;
  1256. sentinel.common.timestamp = 0;
  1257. SDL_PushEvent(&sentinel);
  1258. }
  1259. }
  1260. void SDL_PumpEvents(void)
  1261. {
  1262. SDL_PumpEventsInternal(false);
  1263. }
  1264. // Public functions
  1265. bool SDL_PollEvent(SDL_Event *event)
  1266. {
  1267. return SDL_WaitEventTimeoutNS(event, 0);
  1268. }
  1269. #ifndef SDL_PLATFORM_ANDROID
  1270. static Sint64 SDL_events_get_polling_interval(void)
  1271. {
  1272. Sint64 poll_intervalNS = SDL_MAX_SINT64;
  1273. #ifndef SDL_JOYSTICK_DISABLED
  1274. if (SDL_WasInit(SDL_INIT_JOYSTICK) && SDL_update_joysticks) {
  1275. if (SDL_JoysticksOpened()) {
  1276. // If we have joysticks open, we need to poll rapidly for events
  1277. poll_intervalNS = SDL_min(poll_intervalNS, EVENT_POLL_INTERVAL_NS);
  1278. } else {
  1279. // If not, just poll every few seconds to enumerate new joysticks
  1280. poll_intervalNS = SDL_min(poll_intervalNS, ENUMERATION_POLL_INTERVAL_NS);
  1281. }
  1282. }
  1283. #endif
  1284. #ifndef SDL_SENSOR_DISABLED
  1285. if (SDL_WasInit(SDL_INIT_SENSOR) && SDL_update_sensors && SDL_SensorsOpened()) {
  1286. // If we have sensors open, we need to poll rapidly for events
  1287. poll_intervalNS = SDL_min(poll_intervalNS, EVENT_POLL_INTERVAL_NS);
  1288. }
  1289. #endif
  1290. return poll_intervalNS;
  1291. }
  1292. static int SDL_WaitEventTimeout_Device(SDL_VideoDevice *_this, SDL_Window *wakeup_window, SDL_Event *event, Uint64 start, Sint64 timeoutNS)
  1293. {
  1294. Sint64 loop_timeoutNS = timeoutNS;
  1295. Sint64 poll_intervalNS = SDL_events_get_polling_interval();
  1296. for (;;) {
  1297. int status;
  1298. /* Pump events on entry and each time we wake to ensure:
  1299. a) All pending events are batch processed after waking up from a wait
  1300. b) Waiting can be completely skipped if events are already available to be pumped
  1301. c) Periodic processing that takes place in some platform PumpEvents() functions happens
  1302. d) Signals received in WaitEventTimeout() are turned into SDL events
  1303. */
  1304. SDL_PumpEventsInternal(true);
  1305. SDL_LockMutex(_this->wakeup_lock);
  1306. {
  1307. status = SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST);
  1308. // If status == 0 we are going to block so wakeup will be needed.
  1309. if (status == 0) {
  1310. _this->wakeup_window = wakeup_window;
  1311. } else {
  1312. _this->wakeup_window = NULL;
  1313. }
  1314. }
  1315. SDL_UnlockMutex(_this->wakeup_lock);
  1316. if (status < 0) {
  1317. // Got an error: return
  1318. break;
  1319. }
  1320. if (status > 0) {
  1321. // There is an event, we can return.
  1322. return 1;
  1323. }
  1324. // No events found in the queue, call WaitEventTimeout to wait for an event.
  1325. if (timeoutNS > 0) {
  1326. Sint64 elapsed = SDL_GetTicksNS() - start;
  1327. if (elapsed >= timeoutNS) {
  1328. // Set wakeup_window to NULL without holding the lock.
  1329. _this->wakeup_window = NULL;
  1330. return 0;
  1331. }
  1332. loop_timeoutNS = (timeoutNS - elapsed);
  1333. }
  1334. // Adjust the timeout for any polling requirements we currently have.
  1335. if (poll_intervalNS != SDL_MAX_SINT64) {
  1336. if (loop_timeoutNS >= 0) {
  1337. loop_timeoutNS = SDL_min(loop_timeoutNS, poll_intervalNS);
  1338. } else {
  1339. loop_timeoutNS = poll_intervalNS;
  1340. }
  1341. }
  1342. status = _this->WaitEventTimeout(_this, loop_timeoutNS);
  1343. // Set wakeup_window to NULL without holding the lock.
  1344. _this->wakeup_window = NULL;
  1345. if (status == 0 && poll_intervalNS != SDL_MAX_SINT64 && loop_timeoutNS == poll_intervalNS) {
  1346. // We may have woken up to poll. Try again
  1347. continue;
  1348. } else if (status <= 0) {
  1349. // There is either an error or the timeout is elapsed: return
  1350. return status;
  1351. }
  1352. /* An event was found and pumped into the SDL events queue. Continue the loop
  1353. to let SDL_PeepEvents pick it up .*/
  1354. }
  1355. return 0;
  1356. }
  1357. static SDL_Window *SDL_find_active_window(SDL_VideoDevice *_this)
  1358. {
  1359. SDL_Window *window;
  1360. for (window = _this->windows; window; window = window->next) {
  1361. if (!window->is_destroying) {
  1362. return window;
  1363. }
  1364. }
  1365. return NULL;
  1366. }
  1367. #endif // !SDL_PLATFORM_ANDROID
  1368. bool SDL_WaitEvent(SDL_Event *event)
  1369. {
  1370. return SDL_WaitEventTimeoutNS(event, -1);
  1371. }
  1372. bool SDL_WaitEventTimeout(SDL_Event *event, Sint32 timeoutMS)
  1373. {
  1374. Sint64 timeoutNS;
  1375. if (timeoutMS > 0) {
  1376. timeoutNS = SDL_MS_TO_NS(timeoutMS);
  1377. } else {
  1378. timeoutNS = timeoutMS;
  1379. }
  1380. return SDL_WaitEventTimeoutNS(event, timeoutNS);
  1381. }
  1382. bool SDL_WaitEventTimeoutNS(SDL_Event *event, Sint64 timeoutNS)
  1383. {
  1384. Uint64 start, expiration;
  1385. bool include_sentinel = (timeoutNS == 0);
  1386. int result;
  1387. if (timeoutNS > 0) {
  1388. start = SDL_GetTicksNS();
  1389. expiration = start + timeoutNS;
  1390. } else {
  1391. start = 0;
  1392. expiration = 0;
  1393. }
  1394. // If there isn't a poll sentinel event pending, pump events and add one
  1395. if (SDL_GetAtomicInt(&SDL_sentinel_pending) == 0) {
  1396. SDL_PumpEventsInternal(true);
  1397. }
  1398. // First check for existing events
  1399. result = SDL_PeepEventsInternal(event, 1, SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST, include_sentinel);
  1400. if (result < 0) {
  1401. return false;
  1402. }
  1403. if (include_sentinel) {
  1404. if (event) {
  1405. if (event->type == SDL_EVENT_POLL_SENTINEL) {
  1406. // Reached the end of a poll cycle, and not willing to wait
  1407. return false;
  1408. }
  1409. } else {
  1410. // Need to peek the next event to check for sentinel
  1411. SDL_Event dummy;
  1412. if (SDL_PeepEventsInternal(&dummy, 1, SDL_PEEKEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST, true) &&
  1413. dummy.type == SDL_EVENT_POLL_SENTINEL) {
  1414. SDL_PeepEventsInternal(&dummy, 1, SDL_GETEVENT, SDL_EVENT_POLL_SENTINEL, SDL_EVENT_POLL_SENTINEL, true);
  1415. // Reached the end of a poll cycle, and not willing to wait
  1416. return false;
  1417. }
  1418. }
  1419. }
  1420. if (result == 0) {
  1421. if (timeoutNS == 0) {
  1422. // No events available, and not willing to wait
  1423. return false;
  1424. }
  1425. } else {
  1426. // Has existing events
  1427. return true;
  1428. }
  1429. // We should have completely handled timeoutNS == 0 above
  1430. SDL_assert(timeoutNS != 0);
  1431. #ifdef SDL_PLATFORM_ANDROID
  1432. for (;;) {
  1433. if (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST) > 0) {
  1434. return true;
  1435. }
  1436. Uint64 delay = -1;
  1437. if (timeoutNS > 0) {
  1438. Uint64 now = SDL_GetTicksNS();
  1439. if (now >= expiration) {
  1440. // Timeout expired and no events
  1441. return false;
  1442. }
  1443. delay = (expiration - now);
  1444. }
  1445. Android_PumpEvents(delay);
  1446. }
  1447. #else
  1448. SDL_VideoDevice *_this = SDL_GetVideoDevice();
  1449. if (_this && _this->WaitEventTimeout && _this->SendWakeupEvent) {
  1450. // Look if a shown window is available to send the wakeup event.
  1451. SDL_Window *wakeup_window = SDL_find_active_window(_this);
  1452. if (wakeup_window) {
  1453. result = SDL_WaitEventTimeout_Device(_this, wakeup_window, event, start, timeoutNS);
  1454. if (result > 0) {
  1455. return true;
  1456. } else if (result == 0) {
  1457. return false;
  1458. } else {
  1459. /* There may be implementation-defined conditions where the backend cannot
  1460. * reliably wait for the next event. If that happens, fall back to polling.
  1461. */
  1462. }
  1463. }
  1464. }
  1465. for (;;) {
  1466. SDL_PumpEventsInternal(true);
  1467. if (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST) > 0) {
  1468. return true;
  1469. }
  1470. Uint64 delay = EVENT_POLL_INTERVAL_NS;
  1471. if (timeoutNS > 0) {
  1472. Uint64 now = SDL_GetTicksNS();
  1473. if (now >= expiration) {
  1474. // Timeout expired and no events
  1475. return false;
  1476. }
  1477. delay = SDL_min((expiration - now), delay);
  1478. }
  1479. SDL_DelayNS(delay);
  1480. }
  1481. #endif // SDL_PLATFORM_ANDROID
  1482. }
  1483. static bool SDL_CallEventWatchers(SDL_Event *event)
  1484. {
  1485. if (event->common.type == SDL_EVENT_POLL_SENTINEL) {
  1486. return true;
  1487. }
  1488. return SDL_DispatchEventWatchList(&SDL_event_watchers, event);
  1489. }
  1490. bool SDL_PushEvent(SDL_Event *event)
  1491. {
  1492. if (!event->common.timestamp) {
  1493. event->common.timestamp = SDL_GetTicksNS();
  1494. }
  1495. if (!SDL_CallEventWatchers(event)) {
  1496. SDL_ClearError();
  1497. return false;
  1498. }
  1499. if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
  1500. return false;
  1501. }
  1502. return true;
  1503. }
  1504. void SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
  1505. {
  1506. SDL_EventEntry *event, *next;
  1507. SDL_LockMutex(SDL_event_watchers.lock);
  1508. {
  1509. // Set filter and discard pending events
  1510. SDL_event_watchers.filter.callback = filter;
  1511. SDL_event_watchers.filter.userdata = userdata;
  1512. if (filter) {
  1513. // Cut all events not accepted by the filter
  1514. SDL_LockMutex(SDL_EventQ.lock);
  1515. {
  1516. for (event = SDL_EventQ.head; event; event = next) {
  1517. next = event->next;
  1518. if (!filter(userdata, &event->event)) {
  1519. SDL_CutEvent(event);
  1520. }
  1521. }
  1522. }
  1523. SDL_UnlockMutex(SDL_EventQ.lock);
  1524. }
  1525. }
  1526. SDL_UnlockMutex(SDL_event_watchers.lock);
  1527. }
  1528. bool SDL_GetEventFilter(SDL_EventFilter *filter, void **userdata)
  1529. {
  1530. SDL_EventWatcher event_ok;
  1531. SDL_LockMutex(SDL_event_watchers.lock);
  1532. {
  1533. event_ok = SDL_event_watchers.filter;
  1534. }
  1535. SDL_UnlockMutex(SDL_event_watchers.lock);
  1536. if (filter) {
  1537. *filter = event_ok.callback;
  1538. }
  1539. if (userdata) {
  1540. *userdata = event_ok.userdata;
  1541. }
  1542. return event_ok.callback ? true : false;
  1543. }
  1544. bool SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
  1545. {
  1546. return SDL_AddEventWatchList(&SDL_event_watchers, filter, userdata);
  1547. }
  1548. void SDL_RemoveEventWatch(SDL_EventFilter filter, void *userdata)
  1549. {
  1550. SDL_RemoveEventWatchList(&SDL_event_watchers, filter, userdata);
  1551. }
  1552. void SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
  1553. {
  1554. SDL_LockMutex(SDL_EventQ.lock);
  1555. {
  1556. SDL_EventEntry *entry, *next;
  1557. for (entry = SDL_EventQ.head; entry; entry = next) {
  1558. next = entry->next;
  1559. if (!filter(userdata, &entry->event)) {
  1560. SDL_CutEvent(entry);
  1561. }
  1562. }
  1563. }
  1564. SDL_UnlockMutex(SDL_EventQ.lock);
  1565. }
  1566. void SDL_SetEventEnabled(Uint32 type, bool enabled)
  1567. {
  1568. bool current_state;
  1569. Uint8 hi = ((type >> 8) & 0xff);
  1570. Uint8 lo = (type & 0xff);
  1571. if (SDL_disabled_events[hi] &&
  1572. (SDL_disabled_events[hi]->bits[lo / 32] & (1 << (lo & 31)))) {
  1573. current_state = false;
  1574. } else {
  1575. current_state = true;
  1576. }
  1577. if ((enabled != false) != current_state) {
  1578. if (enabled) {
  1579. SDL_assert(SDL_disabled_events[hi] != NULL);
  1580. SDL_disabled_events[hi]->bits[lo / 32] &= ~(1 << (lo & 31));
  1581. // Gamepad events depend on joystick events
  1582. switch (type) {
  1583. case SDL_EVENT_GAMEPAD_ADDED:
  1584. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_ADDED, true);
  1585. break;
  1586. case SDL_EVENT_GAMEPAD_REMOVED:
  1587. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_REMOVED, true);
  1588. break;
  1589. case SDL_EVENT_GAMEPAD_AXIS_MOTION:
  1590. case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
  1591. case SDL_EVENT_GAMEPAD_BUTTON_UP:
  1592. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_AXIS_MOTION, true);
  1593. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_HAT_MOTION, true);
  1594. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_BUTTON_DOWN, true);
  1595. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_BUTTON_UP, true);
  1596. break;
  1597. case SDL_EVENT_GAMEPAD_UPDATE_COMPLETE:
  1598. SDL_SetEventEnabled(SDL_EVENT_JOYSTICK_UPDATE_COMPLETE, true);
  1599. break;
  1600. default:
  1601. break;
  1602. }
  1603. } else {
  1604. // Disable this event type and discard pending events
  1605. if (!SDL_disabled_events[hi]) {
  1606. SDL_disabled_events[hi] = (SDL_DisabledEventBlock *)SDL_calloc(1, sizeof(SDL_DisabledEventBlock));
  1607. }
  1608. // Out of memory, nothing we can do...
  1609. if (SDL_disabled_events[hi]) {
  1610. SDL_disabled_events[hi]->bits[lo / 32] |= (1 << (lo & 31));
  1611. SDL_FlushEvent(type);
  1612. }
  1613. }
  1614. /* turn off drag'n'drop support if we've disabled the events.
  1615. This might change some UI details at the OS level. */
  1616. if (type == SDL_EVENT_DROP_FILE || type == SDL_EVENT_DROP_TEXT) {
  1617. SDL_ToggleDragAndDropSupport();
  1618. }
  1619. }
  1620. }
  1621. bool SDL_EventEnabled(Uint32 type)
  1622. {
  1623. Uint8 hi = ((type >> 8) & 0xff);
  1624. Uint8 lo = (type & 0xff);
  1625. if (SDL_disabled_events[hi] &&
  1626. (SDL_disabled_events[hi]->bits[lo / 32] & (1 << (lo & 31)))) {
  1627. return false;
  1628. } else {
  1629. return true;
  1630. }
  1631. }
  1632. Uint32 SDL_RegisterEvents(int numevents)
  1633. {
  1634. Uint32 event_base = 0;
  1635. if (numevents > 0) {
  1636. int value = SDL_AddAtomicInt(&SDL_userevents, numevents);
  1637. if (value >= 0 && value <= (SDL_EVENT_LAST - SDL_EVENT_USER)) {
  1638. event_base = (Uint32)(SDL_EVENT_USER + value);
  1639. }
  1640. }
  1641. return event_base;
  1642. }
  1643. void SDL_SendAppEvent(SDL_EventType eventType)
  1644. {
  1645. if (SDL_EventEnabled(eventType)) {
  1646. SDL_Event event;
  1647. event.type = eventType;
  1648. event.common.timestamp = 0;
  1649. switch (eventType) {
  1650. case SDL_EVENT_TERMINATING:
  1651. case SDL_EVENT_LOW_MEMORY:
  1652. case SDL_EVENT_WILL_ENTER_BACKGROUND:
  1653. case SDL_EVENT_DID_ENTER_BACKGROUND:
  1654. case SDL_EVENT_WILL_ENTER_FOREGROUND:
  1655. case SDL_EVENT_DID_ENTER_FOREGROUND:
  1656. // We won't actually queue this event, it needs to be handled in this call stack by an event watcher
  1657. if (SDL_EventLoggingVerbosity > 0) {
  1658. SDL_LogEvent(&event);
  1659. }
  1660. SDL_CallEventWatchers(&event);
  1661. break;
  1662. default:
  1663. SDL_PushEvent(&event);
  1664. break;
  1665. }
  1666. }
  1667. }
  1668. void SDL_SendKeymapChangedEvent(void)
  1669. {
  1670. SDL_SendAppEvent(SDL_EVENT_KEYMAP_CHANGED);
  1671. }
  1672. void SDL_SendLocaleChangedEvent(void)
  1673. {
  1674. SDL_SendAppEvent(SDL_EVENT_LOCALE_CHANGED);
  1675. }
  1676. void SDL_SendSystemThemeChangedEvent(void)
  1677. {
  1678. SDL_SendAppEvent(SDL_EVENT_SYSTEM_THEME_CHANGED);
  1679. }
  1680. bool SDL_InitEvents(void)
  1681. {
  1682. #ifdef SDL_PLATFORM_ANDROID
  1683. Android_InitEvents();
  1684. #endif
  1685. #ifndef SDL_JOYSTICK_DISABLED
  1686. SDL_AddHintCallback(SDL_HINT_AUTO_UPDATE_JOYSTICKS, SDL_AutoUpdateJoysticksChanged, NULL);
  1687. #endif
  1688. #ifndef SDL_SENSOR_DISABLED
  1689. SDL_AddHintCallback(SDL_HINT_AUTO_UPDATE_SENSORS, SDL_AutoUpdateSensorsChanged, NULL);
  1690. #endif
  1691. SDL_AddHintCallback(SDL_HINT_EVENT_LOGGING, SDL_EventLoggingChanged, NULL);
  1692. SDL_AddHintCallback(SDL_HINT_POLL_SENTINEL, SDL_PollSentinelChanged, NULL);
  1693. SDL_InitMainThreadCallbacks();
  1694. if (!SDL_StartEventLoop()) {
  1695. SDL_RemoveHintCallback(SDL_HINT_EVENT_LOGGING, SDL_EventLoggingChanged, NULL);
  1696. return false;
  1697. }
  1698. SDL_InitQuit();
  1699. return true;
  1700. }
  1701. void SDL_QuitEvents(void)
  1702. {
  1703. SDL_QuitQuit();
  1704. SDL_StopEventLoop();
  1705. SDL_QuitMainThreadCallbacks();
  1706. SDL_RemoveHintCallback(SDL_HINT_POLL_SENTINEL, SDL_PollSentinelChanged, NULL);
  1707. SDL_RemoveHintCallback(SDL_HINT_EVENT_LOGGING, SDL_EventLoggingChanged, NULL);
  1708. #ifndef SDL_JOYSTICK_DISABLED
  1709. SDL_RemoveHintCallback(SDL_HINT_AUTO_UPDATE_JOYSTICKS, SDL_AutoUpdateJoysticksChanged, NULL);
  1710. #endif
  1711. #ifndef SDL_SENSOR_DISABLED
  1712. SDL_RemoveHintCallback(SDL_HINT_AUTO_UPDATE_SENSORS, SDL_AutoUpdateSensorsChanged, NULL);
  1713. #endif
  1714. #ifdef SDL_PLATFORM_ANDROID
  1715. Android_QuitEvents();
  1716. #endif
  1717. }