SDL_events.c 48 KB

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