SDL_events.c 69 KB

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