SDL_events.c 61 KB

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