SDL_events.c 66 KB

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